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
|
1 Answer
Reset to default 0I 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.
pre_get_posts
hook, try add thisremove_action('pre_get_posts')
before$terms = get_terms( 'genres' );
– Craig Wayne Commented Jan 23, 2019 at 22:24wp-includes/class-wp-query.php:1634
, it should be the line that hasdo_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