Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
Are you sure that any of the post meets this search criterion? – Rohit Pande Jan 11 at 12:26

2 Answers

up vote 0 down vote accepted

You have to use your object like this :

while ( $query->have_posts() ) : $query->the_post();
share|improve this answer
I just figured that out! Thank you – kallekillen Jan 11 at 12:26
If the snippet is good, can you valid the answer for the others ? :) – Adrien G Jan 11 at 12:30
Done and done, thanks :) – kallekillen Jan 11 at 14:44

Your WP_Query arguments are wrong. posts_per_page is not a part of tax_query. Following should work:

$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' => 'theme',
            'field' => 'slug',
            'terms' => array ('text-image', 'just-text', 'just-image')
        )
    )
);
share|improve this answer
Thanks, that did the trick! – kallekillen Jan 11 at 14:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.