Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WP_Query not able to query multiple post types? WordPress

  • SOLVED

Hello there,

For some reason I'll only get the title for one custom post type, or the other and not both as specified in the $args. Any ideas as to why this could be happening?

[[LINK href="http://pastebin.com/iVHYEs69"]]Here's my code.[[/LINK]]
[[LINK href="http://wordpress.stackexchange.com/a/60715"]]Another example of exactly what's going on[[/LINK]], but I don't have WPML.

I tried uninstalling all plugins and turned errors on. No luck.

Thanks for your help!

Answers (2)

2014-09-06

Luis Abarca answers:

Can you paste the code of the custom post type declaration ?

Both post types have published content ?

Try this to see all posts from the query.

<?php

$args = array(
'post_type' => array('videos','images'),
'posts_per_page' => -1 // show all posts
);

$Justified_Query = new WP_Query( $args );
?>
<?php if ( $Justified_Query->have_posts() ) : ?>
<?php
while ( $Justified_Query->have_posts() ) :
$Justified_Query->the_post();
?>
<?php the_title();?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>


Chris comments:

Thank you Luis,

For some reason it now works. I was playing around with posts_per_page and a bunch parameters before. I did try that and it didn't work before. Now it strangely does and everything is working as it should? I'm confused to say the least.

Here's the code for my [[LINK href="http://pastebin.com/5C8W2Vs2"]]post types[[/LINK]] if you want to take a look. Maybe offer some suggestions as to what could of been going on because I'm still learning, so it will still help me to understand.

Thanks again for your help!


Luis Abarca comments:

Well everything looks good, maybe a cache plugin ?

Did you flush the rewrite rules ?

You can also call both functions with one call.


add_action('init', 'my_cpt_init');

function my_cpt_init()
{
videos_init();
images_init();
flush_rewrite_rules(); // this should be called once (on plugin activation).
}

2014-09-07

Hariprasad Vijayan answers:

Hello,

By default query post will display most recent posts, i think you may not have any new posts in other post type. Just try to add posts to both post types and check.

Regards,
Hariprasad