最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - posts_per_page is not working by term

programmeradmin0浏览0评论

I'm trying to limit the posts, but it does not work. It shows them all.

Why posts_per_page does not work here?

<?php // Output all Taxonomies names with their respective items
$terms = get_terms( 'genres' );
foreach( $terms as $term ):
?>
    <h3><?php echo $term->name; // Print the term name ?></h3>
    <ul>
        <?php
        $posts = get_posts( array(
            'post_type'      => 'product',
            'posts_per_page' => 1, 
            'taxonomy'       => $term->taxonomy,
            'term'           => $term->slug,                                  
        ) );
        foreach ( $posts as $post):  // begin cycle through posts of this taxonmy
            setup_postdata( $post ); // set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
        ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endforeach; ?>
    </ul>

I'm trying to limit the posts, but it does not work. It shows them all.

Why posts_per_page does not work here?

<?php // Output all Taxonomies names with their respective items
$terms = get_terms( 'genres' );
foreach( $terms as $term ):
?>
    <h3><?php echo $term->name; // Print the term name ?></h3>
    <ul>
        <?php
        $posts = get_posts( array(
            'post_type'      => 'product',
            'posts_per_page' => 1, 
            'taxonomy'       => $term->taxonomy,
            'term'           => $term->slug,                                  
        ) );
        foreach ( $posts as $post):  // begin cycle through posts of this taxonmy
            setup_postdata( $post ); // set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
        ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endforeach; ?>
    </ul>
Share Improve this question edited Jan 23, 2019 at 22:43 Kashif Rafique 2351 gold badge3 silver badges12 bronze badges asked Jan 23, 2019 at 21:02 MaxMax 536 bronze badges 5
  • could possibly be an external interference, usually its the pre_get_posts hook, try add this remove_action('pre_get_posts') before $terms = get_terms( 'genres' ); – Craig Wayne Commented Jan 23, 2019 at 22:24
  • @CraigWayne it does not work, it also shows an error Warning: Missing argument 2 for remove_action(), called in front-page.php on line 228 and defined in wp-includes\plugin.php on line 554 – Max Commented Jan 23, 2019 at 22:39
  • Try comment out this line in wp-includes/class-wp-query.php:1634, it should be the line that has do_action_ref_array( 'pre_get_posts', array( &$this ) ); let me know if that helps, otherwise, try disabling all your plugins – Craig Wayne Commented Jan 23, 2019 at 23:16
  • it does not work, and the only plugin I have is contact form 7 – Max Commented Jan 23, 2019 at 23:50
  • Where is this code located and how is it being called? Your post loop is inside another loop, are you expecting to only see a single post on the page? – Tom J Nowell Commented Jan 24, 2021 at 20:59
Add a comment  | 

1 Answer 1

Reset to default 0

I think you are using the tax query in wrong way. Please use 'tax_query' => array( array( 'taxonomy' => '$term->taxonomy', 'field' => 'slug', 'terms' => '$term->slug' ) ),

instead of 'taxonomy' => $term->taxonomy, 'term' => $term->slug,

It will work fine.

发布评论

评论列表(0)

  1. 暂无评论