admin管理员组

文章数量:1325147

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