2

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!

1
  • Are you sure that any of the post meets this search criterion? Jan 11, 2013 at 12:26

2 Answers 2

0

You have to use your object like this :

while ( $query->have_posts() ) : $query->the_post();
1
  • If the snippet is good, can you valid the answer for the others ? :)
    – aguidis
    Jan 11, 2013 at 12:30
1

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')
        )
    )
);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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