(I realize you’re leaning away from this, but maybe if you can get it working, it’s worthwhile. With the recent perfomance improvements in 3.4 for WP_Query, this could be worthwhile.)
WP_Query
is the right decision if this is a secondary loop. Otherwise, you might look into pre_get_posts
.
When you use WP_Query
make sure that:
- You don’t use a reserved global variable to save your new query. (For instance, a lot of people cause problems by saving their new query as
$wp_query
and then they overwrite their existing query object. - You use the right loop format e.g.
$my_custom_query->have_posts();
- Finally, rather than using
tag
or thetax
var (which is deprecated), use a tax_query.
Looking at your example, here’s a suggested loop:
$food_query_args = array(
'post_type' => 'projects',
'tax_query' => array(
array(
'taxonomy' => 'post_tag', // or the name of your custom taxonomy
'field' => 'slug',
'terms' => 'food-and-beverage' // FYI, it's more stable imho to use the ID if you can. If you do, that switch 'slug' in the preceding line to 'id'
)
)
);
$food_query = new WP_Query( $food_query_args );
if( $food_query->have_posts() ) : while( $food_query->have_posts() ) : $food_query->the_post();
// STUFF
endwhile; endif; wp_reset_postdata();