2

I'm trying to sort my posts in the way I stated in the title.

What's the simplest way to do this?

I know how to change the order by object using the pre_get_post hook. But how can I use it to get what I need?

EDIT: here's my code so far

add_action('pre_get_posts', 'filter_category_orderby');

function filter_category_orderby($query){
if($query->is_category()){

    $query2 = $query;
    $query->set('orderby','rand');

    $wp_query = new WP_Query();
    $wp_query->posts = array_merge( $query2->posts, $query->posts );


    $wp_query->post_count = $query1->post_count + $query2->post_count;
    $query = $wp_query;
 }
}

This is the idea, but i can't seem to make it work like i want it to. got the idea from here

  • 1
    Two loops: "Show 3 most recent posts" (keep IDs in mind) and "Show all except 3 most recent IDs, 'orderby'=>'rand'"? – Max Yudin Feb 18 '19 at 16:06
  • Hello @MaxYudin thanks for the response. Can I do this and just attach it to the pre_get_post hook? Also do you have any example I can see to fully understand what you mean? – kberStill Feb 18 '19 at 16:22
  • keep in mind, that with order random you might get in trouble when paging.. plus i don't like the idea of using 2 queries.. but right now i'm not coming up with a better solution, eather. sorry.. – honk31 Feb 18 '19 at 17:47
0

EDIT: Here is the correct answer...

<!-- RECENT BLOG POSTS -->
<?php
$recentargs = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category_name' => 'social-grid',
    'posts_per_page' => $number_of_recent_posts,
    'orderby' => 'post_date',
    'order' => 'DESC'
);
$rec_arr_posts = new WP_Query( $recentargs );

if ( $rec_arr_posts->have_posts() ) :

    while ( $rec_arr_posts->have_posts() ) :
        $rec_arr_posts->the_post();
    ?>

        <?php get_the_post_thumbnail_url(); ?>
        <?php the_permalink(); ?>
        <?php the_title(); ?>
        <?php echo get_the_excerpt(); ?>

    <?php endwhile; ?>

<?php endif; ?>
<?php wp_reset_postdata(); ?>
<!-- END RECENT BLOG POSTS -->  

<!-- RECENT EXCLUDED POSTS -->
<?php
$recenttoexcludeargs = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category_name' => 'social-grid',
    'posts_per_page' => $number_of_recent_posts,
    'orderby' => 'post_date',
    'order' => 'DESC'
);
$recent_to_exclude_arr_posts = new WP_Query( $recenttoexcludeargs );

if ( $recent_to_exclude_arr_posts->have_posts() ) {
    $posts_ids = wp_list_pluck( $recent_to_exclude_arr_posts->posts, 'ID' );
} ?>
<!-- END RECENT EXCLUDED POSTS -->
<!-- RANDOM BLOG POSTS -->
<?php
$randomargs = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category_name' => 'social-grid',
    'posts_per_page' => $number_of_random_posts,
    'post__not_in' => $posts_ids,
    'orderby' => 'rand',
    'order' => 'DESC'
);
$rand_arr_posts = new WP_Query( $randomargs );

if ( $rand_arr_posts->have_posts() ) :

    while ( $rand_arr_posts->have_posts() ) :
        $rand_arr_posts->the_post();
    ?>

        <?php get_the_post_thumbnail_url(); ?>
        <?php the_permalink(); ?>
        <?php the_title(); ?>
        <?php echo get_the_excerpt(); ?>

    <?php endwhile; ?>

<?php endif; ?>
<?php wp_reset_postdata(); ?>

You need to set the variables $number_of_recent_posts & $number_of_random_posts.

P.S - where do you work? - I am coming for your job c:

  • Provide the code you have used so far and we can help you tweak it... – Jason Is My Name Feb 18 '19 at 16:36
  • Have a look at my edit, see if you can apply these to your use cases, have a go. Come back if you are still struggling. – Jason Is My Name Feb 18 '19 at 17:02
  • Hello, I'm sorry, for asking this. but can I implement your code in the function.php of my theme? – kberStill Feb 18 '19 at 17:22
  • I am working on a similar project, I will give you the correct code at the end. You shouldn't need to do too much in your functions. You could just write this as a custom query within whatever page you're working on - or create it as an included file that you could pull into other pages too. – Jason Is My Name Feb 19 '19 at 10:06
  • I have striped out the crud and given the correct answer in my answer c: – Jason Is My Name Feb 19 '19 at 16:26

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.