I’m just getting used to the WP Query and was hoping I could get some assistance on this.
I’ve created a custom taxonomy (theme) and now want to display the latest post with one of these taxonomies on my front page as a top featured post.
Now I can’t really seem to work out how to get it to filter the query properly, maybe someone can correct me:
<?php
$args = array(
'tax_query' => array(
array(
'posts_per_page' => 1,
'taxonomy' => 'theme',
'field' => 'slug',
'terms' => array ('text-image', 'just-text', 'just-image')
)
)
);
$query = new WP_Query( $args );
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Any help would be highly appreciated, thanks!
EDIT:
Here’s the finished code in case anyone else needs it;
<?php
$args = array(
'post_type' => 'post', // it's default, you can skip it
'posts_per_page' => '1',
'order_by' => 'date', // it's also default
'order' => 'DESC', // it's also default
'tax_query' => array(
array(
'taxonomy' => 'nameoftaxonomy',
'field' => 'slug',
'terms' => array ('whatever1', 'whatever2', 'whatever3')
)
)
);
$query = new WP_Query( $args );
?>
<?php if (have_posts()) : while( $query->have_posts() ) : $query->the_post(); ?>
Thanks for the help!
You have to use your object like this :
Your
WP_Query
arguments are wrong.posts_per_page
is not a part oftax_query
. Following should work: