Specify number of posts in my ‘tax_query’

I’m showing posts of the term ‘football’ belonging to the taxonomy ‘section’, and everything works perfectly but
Where can I specify the number of posts I want to show?

just like this: ‘showposts’ => 3

Read More

I do not know where to put it

    $myquery['tax_query'] = array(
                   array(
                        'taxonomy' => 'section',
                        'terms' => array('futbol'),
                        'field' => 'slug',
                    ),
        );
        query_posts($myquery);

Related posts

Leave a Reply

3 comments

  1. Never user query_posts, under any circumstances. Use WP_Query instead, which is how query_posts works internally, but without the trickery and downsides.

    You’ll also find that the WP_Query documentation gives you explanations for every parameter, including what you are trying to do:

    posts_per_page (int) – number of post to show per page (available with Version 2.1, replaced showposts parameter). Use
    'posts_per_page'=>-1 to show all posts (the ‘offset‘ parameter is
    ignored with a -1 value). Set the ‘paged’ parameter if pagination is
    off after using this parameter. Note: if the query is in a feed,
    wordpress overwrites this parameter with the stored ‘posts_per_rss
    option. To reimpose the limit, try using the ‘post_limits‘ filter, or
    filter ‘pre_option_posts_per_rss‘ and return -1

    Including an example:

    Show x Posts per page

    Display 3 posts per page:

    $query = new WP_Query( 'posts_per_page=3' );
    

    or as i would put it:

    $args = array(
        'posts_per_page' => 3
    );
    $query = new WP_Query( $args );
    
    if ( $query->have_posts() ) {
        while( $query->have_posts() ) {
            $query->the_post();
            // display the post
        }
    }
    

    Notice how the main loop is the same as when using query_posts, only I’ve added $query-> to the beginning of have_posts and the_post?

    You can now modify the parameters to add in your tax query

    $args = array(
        'posts_per_page' => 3,
        'tax_query' => array(
            ...etc
        )
    );
    

    http://codex.wordpress.org/Class_Reference/WP_Query

  2. You want posts_per_page instead:

    $myquery = array(
        'posts_per_page' => 3,
        'tax_query' => array(
            array(
                'taxonomy' => 'section',
                'terms' => array( 'futbol' ),
                'field' => 'slug',
            ),
        ),
    );
    
  3. This works, thanks Tom 🙂

            <?php
            $args = array(
                'posts_per_page' => 2,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'section',
                        'field' => 'slug',
                        'terms' => array('futbol'),
                             )
                    )
                );
            $query = new WP_Query( $args );
    
           if ( $query->have_posts() ) {
                while( $query->have_posts() ) {
                    $query->the_post();
    
             //The Post
    
                }
            }
            ?>