Quantcast
Channel: WordPress.org Forums » All Posts
Viewing all articles
Browse latest Browse all 113016

Andrew Bartel on "Template to Display All Posts in Custom Post Type and Custom Taxonomy"

$
0
0

So, you need to set up a secondary query. If you see in your code, you're using the main query:
<?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>

which is why I asked about the reading settings.

At the top of your template file, instantiate a new instance of the WP_Query object and pass it the necessary parameters, your custom post type and the number of posts you want to display:

$args = array('post_type' => 'work','post_count'=>whatever);
$my_query = new WP_Query($args);

You can further refine this query string (the $args array) if you want. Take a look at the documentation for WP_Query. Now, you can use $my_query as a substitute loop.

<?php if ( $my_query->have_posts() ): while ( $my_query->have_posts() ): the_post(); ?>

So, when you call the_post() in that while loop, you're referencing the secondary loop you created, rather than the main loop. Understand?

You might find this talk on the loops and queries useful too: http://wordpress.tv/2012/06/15/andrew-nacin-wp_query/


Viewing all articles
Browse latest Browse all 113016

Trending Articles