简体   繁体   中英

Display custom registered posts in the custom query new WP_Query

I'm building a custom plugin for WordPress to display some custom posts. So far so good, it worked (I had to re-save the permalinks), and now I'm able to see it in my archive page and, since I added the slug in the array for the register_post_type function, I can see them adding the slug in the url (for example: mywebsite.com/coolposts:

'rewrite' => array( 'slug' => 'coolposts' ),

So far so good but I have a problem and I don't know how to solve it.

I want to display my custom posts "coolposts" in my custom query:

$the_query = new WP_Query('orderby=title&order=asc&posts_per_page=50'); 
while ($the_query->have_posts()) :
$the_query->the_post();

if ((($the_query->current_post+1) % 3 == 0) && ($the_query->current_post+1 !== count($the_query->posts))):
echo "</div><div class='row'>";

endif;
endwhile;

Because, for some reason, my custom posts are excluded from the main query.

Am I doing something wrong?

Just to be clear, I don't want to display ONLY my customs posts, I want to display all of my posts.

I assume you registered custom post type with the name coolposts when registered with register_post_type function. If so then just add another param post_type=coolposts . To avoid unexpected situation you can add one more param post_status=publish , this param will ensure only published posts are displayed.

$the_query = new WP_Query('orderby=title&order=asc&posts_per_page=50&post_type=coolposts');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM