Set posts_per_page in WP_Query for custom taxonomy

I’m using the below code to get all the pages under the custom taxonomy terms, and it shows all the pages. What i want is to limit this and gets only say the last two pages published under the custom taxonomy.

The posts_per_page=1 is not working here and show all pages.

Read More
<?php
$post_type = 'page';

// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );

foreach( $taxonomies as $taxonomy ) : 

// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );

echo '<div id="activities_categories">';
foreach( $terms as $term ) :

$customposts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=1" );
$customposts->set('posts_per_page', 1);
if( $customposts->have_posts() ):
    while( $customposts->have_posts() ) : $customposts->the_post();
        echo '<li>';
        echo '<a href="' . get_permalink() . '">' . get_the_title(); 
        echo '</a></li>';
    endwhile;

endif;

endforeach;

echo '</div>';
endforeach;
 ?>

Thanks

Edit

The right code to solve the problem

<?php
        $post_type = 'page';

        // Get all the taxonomies for this post type
        $taxonomies = get_object_taxonomies($post_type); // names (default)

        foreach( $taxonomies as $taxonomy ) : 

            // Gets every "category" (term) in this taxonomy to get the respective posts
             $terms = get_terms( $taxonomy ); 

            echo '<ul>';
            foreach( $terms as $term ) :

            $args = array(
            'showposts' => 1,
            'tax_query' => array(
                array(
                    'taxonomy' => $taxonomy,
                'terms' => $term->slug,
                'field' => 'slug'
                )
            )
            );

            $customposts = new WP_Query( $args );

            if( $customposts->have_posts() ):
                while( $customposts->have_posts() ) : $customposts->the_post();
                    echo '<li>';
                    echo '<a href="' . get_permalink() . '">' . get_the_title(); 
                    echo '</a></li>';
                endwhile;

            endif;

            endforeach;

            echo '</ul>';
        endforeach;
    ?>

Related posts

Leave a Reply

2 comments

  1. Your WP_Query is formed incorrectly. See WP_Query Taxonomy Parameters.

    Try this code:

    <?php
    $post_type = 'page';
    // Get all the taxonomies for this post type
    $taxonomies = get_object_taxonomies($post_type); // names (default)
    foreach( $taxonomies as $taxonomy ) : 
        // Gets every "category" (term) in this taxonomy to get the respective posts
        $terms = get_terms( $taxonomy ); 
        echo '<div id="activities_categories">';
        foreach( $terms as $term ) :
            $customposts = new WP_Query(
               array(
                  'posts_per_page' => 1,
                  'tax_query' => array(
                       array(
                           'taxonomy' => $taxonomy,
                           'terms' => $term->slug,
                           'field' => 'slug'
                       )
                  )
               )
            );
            if( $customposts->have_posts() ):
                while( $customposts->have_posts() ) : $customposts->the_post();
                    echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
                endwhile;
            endif;
        endforeach;
        echo '</div>';
    endforeach;
    
  2. The problem is not where you think it is. Run this (I changed post_type to “posts” so that it would work conveniently for me, but it shouldn’t matter):

    var_dump('begin #################################################');
    $post_type = 'post';
    
    // Get all the taxonomies for this post type
    $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
    
    foreach( $taxonomies as $taxonomy ) : 
    
      // Gets every "category" (term) in this taxonomy to get the respective posts
      $terms = get_terms( $taxonomy );
      // var_dump('raw_terms',$terms);
      echo '<div id="activities_categories">';
      foreach( $terms as $term ) :
    
        $customposts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=1" );
        // debuggging
    //     var_dump('customposts_all',$customposts); 
    //     var_dump('customposts_posts',$customposts->posts); 
        var_dump('customposts_count',count($customposts->posts)); 
        // debugging
        $customposts->set('posts_per_page', 1);
        if( $customposts->have_posts() ):
        while( $customposts->have_posts() ) : $customposts->the_post();
            echo '<li>';
            echo '<a href="' . get_permalink() . '">' . get_the_title(); 
            echo '</a></li>';
        endwhile;
    
        endif;
    
      endforeach;
      var_dump('middle #################################################');
      echo '</div>';
    endforeach;
    var_dump('end #################################################');
    

    Now, look for ‘customposts_counts’ in your generated page. You have one result every time.

    I think the problem is more with the logic of your code, with the logic of your nested loops. You are looping, at least potentially, over the same terms many times. You will have to make an effort to prevent those duplicates. See if this work for you:

    var_dump('begin #################################################');
    $post_type = 'post';
    
    // Get all the taxonomies for this post type
    $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
    // var_dump($taxonomies);
    $unique_terms = array();
    foreach( $taxonomies as $taxonomy ) {
      // Gets every "category" (term) in this taxonomy to get the respective posts
      $terms = get_terms( $taxonomy );
      // var_dump('raw_terms',$terms);
      foreach( $terms as $term ) {
        // var_dump('one_term',$term);
        $unique_terms[$term->term_id] = $term;
      }
    }
    // var_dump('unique_terms',$unique_terms);
    
    $dupl = array();
    foreach ($unique_terms as $t) {
      // var_dump('dupl',$dupl);
      $args = array(
          'tax_query' => array(
          'taxonomy' => $t->taxonomy,
          'term' => $t->slug,
          'field' => 'slug'
        ),
        'posts_per_page' => 1
      );
      if (!empty($dupl)) $args['post__not_in'] = $dupl;
      $customposts = new WP_Query($args);
      // var_dump('customposts_all',$customposts); 
      // var_dump('customposts_posts',$customposts->posts); 
      // var_dump('customposts_count',count($customposts->posts)); 
      if( $customposts->have_posts() ) {
        while( $customposts->have_posts() ) {
          echo '<div id="activities_categories">';
        $customposts->the_post();
        $dupl[] = $post->ID;
        // var_dump('one_post',$post);
        echo '<li>';
        echo '<a href="' . get_permalink() . '">' . get_the_title(); 
        echo '</a></li>';
          echo '</div>';
        }
      }
    }
    
    var_dump('end #################################################');