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
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: