admin管理员组文章数量:1314282
My site has a "Portfolio" page, which pulls in posts in category 3. Here is my code which selects the posts:
<?php
$args = array('cat' => 3,
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC');
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
<div class="portfolio-item">
<?php the_post_thumbnail() ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
<?php endforeach; ?>
But the posts appear in a random order each time the page is refreshed, even though I have 'orderby' and 'order' in the arguments. There seems to be a strange interaction with a short section of code in functions.php which looks like this;
<?php
add_action('pre_get_posts','alter_query');
function alter_query($query){
if ($query->is_main_query() && is_home())
$query->set('cat', '2');
$query->set('orderby', 'rand');
}
?>
This code shows a random post from all posts of category 2 on the homepage. Whilst this line is included;
$query->set('cat', '2');
The orderby => date and order => DESC stop working on the portfolio page, but I need that line to select only category 2 posts for the homepage. If anyone could point me in the direction of why this might be happening I'd be very grateful. Thanks
My site has a "Portfolio" page, which pulls in posts in category 3. Here is my code which selects the posts:
<?php
$args = array('cat' => 3,
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC');
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
<div class="portfolio-item">
<?php the_post_thumbnail() ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
<?php endforeach; ?>
But the posts appear in a random order each time the page is refreshed, even though I have 'orderby' and 'order' in the arguments. There seems to be a strange interaction with a short section of code in functions.php which looks like this;
<?php
add_action('pre_get_posts','alter_query');
function alter_query($query){
if ($query->is_main_query() && is_home())
$query->set('cat', '2');
$query->set('orderby', 'rand');
}
?>
This code shows a random post from all posts of category 2 on the homepage. Whilst this line is included;
$query->set('cat', '2');
The orderby => date and order => DESC stop working on the portfolio page, but I need that line to select only category 2 posts for the homepage. If anyone could point me in the direction of why this might be happening I'd be very grateful. Thanks
Share Improve this question asked Nov 24, 2020 at 17:01 tomtomthompsontomtomthompson 132 bronze badges1 Answer
Reset to default 0If the given code is correct it might be the missing brackets on your pre_get_posts
conditional. Let's format it with proper indentation and spacing:
function alter_query( $query ) {
if( $query->is_main_query() && is_home() )
$query->set( 'cat', '2' );
$query->set( 'orderby', 'rand' );
}
add_action( 'pre_get_posts','alter_query' );
Because there's no brackets for your conditional statement, it will run the first line inside the conditional and all other lines outside. It's shorthand. To fix this we just need to add brackets:
function alter_query( $query ) {
if( $query->is_main_query() && is_home() ) {
$query->set( 'cat', '2' );
$query->set( 'orderby', 'rand' );
}
}
add_action( 'pre_get_posts','alter_query' );
By encapsulating the additional functionality in brackets we ensure that all statements are run only if that condition is met.
本文标签: functionsOrdering posts by publish date not working
版权声明:本文标题:functions - Ordering posts by publish date not working? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741963855a2407432.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论