admin管理员组文章数量:1325231
I'm trying to use orderby => rand
on a custom taxonomy page, but it's not working correctly.
On other pages I use below piece of code to show my posts from a custom post type:
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => 12,
'post_type' => 'mycustomposttype',
'orderby' => 'rand(1234)',
'order' => 'ASC',
'paged' => $paged,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<div class="pagintion">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
?>
</div>
<?php endif; ?>
As you can see, I'm using rand(1234)
instead of just rand
. This is because I need the pagination to work correctly in combination with the random posts functionality. I've got this solution from
Now, on my custom taxonomy page (taxonomy-mycustomtaxonomy.php
) I'm using the default WP loop and modifying the query in functions.php
with pre_get_posts
& $query->set
. This is what I have now:
function alter_custom_tax_query( $query ) {
if ( !is_admin() && $query->is_tax('mycustomtaxonomy') && $query->is_main_query() ) {
$query->set( 'post_type', array( 'mycustomposttype' ) );
$query->set( 'posts_per_page', '1' );
$query->set( 'orderby', 'rand(1234)' );
}
}
add_action( 'pre_get_posts', 'alter_custom_tax_query' );
Here, the rand
function is working, but when I try to use rand(1234)
it stops working. Can anyone help me with this? I can't seem to figure it out.
Thanks in advance!
本文标签: custom taxonomyRandom order not working correctly when using default loopquerygtset
版权声明:本文标题:custom taxonomy - Random order not working correctly when using default loop + $query->set 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742170145a2426568.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论