0

Currently my homepage posts shows as random and arranged by visitor session but in category it arranged by last added, I want to arrange posts random in category similar to homepage

function filter_where($where = '') {
    $duration = '-' . of_get_option('frontpage_popularity_duration') . ' days';
    $where .= " AND post_date > '" . date('Y-m-d', strtotime($duration)) . "'";
    if (of_get_option('frontpage_popularity') == 'comments') {
        $where .= ' AND comment_count != 0';
    }
    return $where;
}

function random_posts_orderby($orderby_statement) {
    $seed = $_SESSION["pinc_rand"];

    if (empty($seed)) {
        $_SESSION["pinc_rand"] = rand();
    }

    $orderby_statement = 'RAND('.$seed.')';
    return $orderby_statement;  
}

if (is_home()) {
    if ('likes' == $popularity) {
        if (of_get_option('show_repins') != 'disable') {
            $args = array(
                'meta_key' => '_Likes Count',
                'meta_compare' => '>',
                'meta_value' => '0',
                'orderby' => 'meta_value_num',
                'order' => 'DESC',
                'paged' => $paged
            );              
        } else {
            $args = array(
                'meta_key' => '_Likes Count',
                'meta_compare' => '>',
                'meta_value' => '0',
                'orderby' => 'meta_value_num',
                'order' => 'DESC',
                'paged' => $paged,
                'meta_query' => array(
                    'relation' => 'OR',
                    array(
                        'key' => '_Original Post ID',
                        'compare' => 'NOT EXISTS'
                    ),
                    array(
                        'key' => '_Original Post ID',
                        'value' => 'deleted'
                    )
                )
            );
        }
        add_filter('posts_where', 'filter_where');
        add_filter('posts_orderby', 'pinc_meta_value_num_orderby');
        query_posts($args);
        remove_filter('posts_where', 'filter_where');
        remove_filter('posts_orderby', 'pinc_meta_value_num_orderby');
    } else if ($popularity == 'repins') {   
        if (of_get_option('show_repins') != 'disable') {
            $args = array(
                'meta_key' => '_Repin Count',
                'meta_compare' => '>',
                'meta_value' => '0',
                'orderby' => 'meta_value_num',
                'order' => 'DESC',
                'paged' => $paged
            );
        } else {
            $args = array(
                'meta_key' => '_Repin Count',
                'meta_compare' => '>',
                'meta_value' => '0',
                'orderby' => 'meta_value_num',
                'order' => 'DESC',
                'paged' => $paged,
                'meta_query' => array(
                    'relation' => 'OR',
                    array(
                        'key' => '_Original Post ID',
                        'compare' => 'NOT EXISTS'
                    ),
                    array(
                        'key' => '_Original Post ID',
                        'value' => 'deleted'
                    )
                )
            );
        }
        add_filter('posts_where', 'filter_where');
        add_filter('posts_orderby', 'pinc_meta_value_num_orderby');
        query_posts($args);
        remove_filter('posts_where', 'filter_where');
        remove_filter('posts_orderby', 'pinc_meta_value_num_orderby');
    } else if ($popularity == 'comments') { 
        if (of_get_option('show_repins') != 'disable') {
            $args = array(
                'orderby' => 'comment_count',
                'paged' => $paged
            );
        } else {
            $args = array(
                'orderby' => 'comment_count',
                'paged' => $paged,
                'meta_query' => array(
                    'relation' => 'OR',
                    array(
                        'key' => '_Original Post ID',
                        'compare' => 'NOT EXISTS'
                    ),
                    array(
                        'key' => '_Original Post ID',
                        'value' => 'deleted'
                    )
                )
            );
        }
        add_filter('posts_where', 'filter_where');
        add_filter('posts_orderby', 'pinc_comments_orderby');
        query_posts($args);
        remove_filter('posts_where', 'filter_where');
        remove_filter('posts_orderby', 'pinc_comments_orderby');
    } else if ($popularity == 'random') {           
        if (of_get_option('show_repins') != 'disable') {
            $args = array(
                'paged' => $paged
            );
        } else {
            $args = array(
                'paged' => $paged,
                'meta_query' => array(
                    'relation' => 'OR',
                    array(
                        'key' => '_Original Post ID',
                        'compare' => 'NOT EXISTS'
                    ),
                    array(
                        'key' => '_Original Post ID',
                        'value' => 'deleted'
                    )
                )
            );
        }
        add_filter('posts_orderby', 'random_posts_orderby');
        query_posts($args);
        remove_filter('posts_orderby', 'random_posts_orderby');
    } else {
        if (of_get_option('show_repins') != 'disable') {
            $args = array(
                'paged' => $paged
            );
        } else {
            $args = array(
                'paged' => $paged,
                'meta_query' => array(
                    'relation' => 'OR',
                    array(
                        'key' => '_Original Post ID',
                        'compare' => 'NOT EXISTS'
                    ),
                    array(
                        'key' => '_Original Post ID',
                        'value' => 'deleted'
                    )
                )
            );  
        }
        query_posts($args);
    }
}

if (is_category()) {
    $categories = array(get_query_var('cat'));
    $subcats = get_categories(array('child_of' => get_query_var('cat')));
    if ($subcats) {
        foreach ($subcats as $subcat) {
            array_push($categories, $subcat->cat_ID);
        }
    }

    if (of_get_option('show_repins') != 'disable') {            
        $args = array(
            'category__in' => $categories,
            'paged' => $paged
        );
    } else {
        $args = array(
            'category__in' => $categories,
            'paged' => $paged,
            'meta_query' => array(
                'relation' => 'OR',
                array(
                    'key' => '_Original Post ID',
                    'compare' => 'NOT EXISTS'
                ),
                array(
                    'key' => '_Original Post ID',
                    'value' => 'deleted'
                )
            )
        );  
    }
    query_posts($args); 
}

if (is_tag()) {
    if (of_get_option('show_repins') != 'disable') {
        $args = array(
            'tag__in' => get_query_var('tag_id'),
            'paged' => $paged
        );
    } else {
        $args = array(
            'tag__in' => get_query_var('tag_id'),
            'paged' => $paged,
            'meta_query' => array(
                'relation' => 'OR',
                array(
                    'key' => '_Original Post ID',
                    'compare' => 'NOT EXISTS'
                ),
                array(
                    'key' => '_Original Post ID',
                    'value' => 'deleted'
                )
            )
        );  
    }
    query_posts($args); 
}

get_template_part('index', 'masonry');
get_footer();

?>

and this category page code

  • I would note that ordering by random and anything random is going to be expensive/slower, and that doing this means it's impossible to cache your homepage. Also querying for posts by their post meta does not scale and is horrifyingly slow. Post meta isn't meant to be used for searching and querying for posts, it's optimised for when you already know which posts you want. SESSION will also have issues on some web hosts, e.g. this functionality won't work on WP Engine – Tom J Nowell Jun 28 '17 at 14:54
  • I am trying to put 'orderby' => 'rand', in case of category but it is no action – user3415122 Jun 28 '17 at 14:58
  • OK , I am trying to show posts ordered by random only not by session, But how ? – user3415122 Jun 28 '17 at 15:07
0

You don't need complicated filters to modify the SQL query, just use the order parameter of WP_Query ( avoid query_posts, your code above will trigger multiple unused queries, making the page very slow )

$q = new WP_Query( [
    'order' => 'rand'
] );

You can pass an integer seed too:

$q = new WP_Query( [
    'order' => 'rand(5)'
] );

But there are significant caveats

Performance

Random is slooooow, the database has to do a lot of extra work. Instead, it'd be much faster to randomly pick something to search for in PHP, then search for that instead

Other Performance Problems

The code in your question uses query_posts, and at times will overwrite the main query, then overwrite the new query, then overwrite it yet again. A single query_posts call after all the checks would be significantly faster. A pre_get_posts filter would be even faster ( eliminates the original WP query that's never used ).

You're also relying heavily on post meta queries. Aside from random ordering, this is the worst thing you can do for performance and scaling on a WordPress site. I expect your sites server will take 5-10 seconds to load the homepage, possibly longer, with significant SEO and visitor retention issues as a result. You're also going to face major issues serving pages to multiple users, the database will struggle to perform those queries for more than 4 or 5 concurrent visitors. Your host is likely to contact you as a result for high CPU usage, or you'll need a lot of hosting hardware

It will also be impossible to cache what you're doing. Full page caching systems will not work with this, neither will object caching, and browser cache may even get in your way.

Instead of showing 10 random posts, try showing the 10 oldest posts, or the 10 newest posts, or 10 posts from a randomly selected year, or the most commented on posts, or the most popular. Decide which one in PHP and cache the result. Choosing from 10 pre-selected queries is another option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.