0

English is not my first language, sorry :)

I need differend per paginate page sorting.

For exemple, I created the loop:

 $qargs = array(
                    'posts_per_page' => 15,
                    'no_found_rows'  => true,
                    'order' => 'DESC',
                    'post_type' => 'post',
                    'suppress_filters' => false,
                    'orderby' => 'post_views', // here I order my posts by views
                    'fields' => '',
                    'date_query' => array(
                        array(
                            'after'     => '-7 days',
                            'column' => 'post_date',
                        ),
                    ),
posts_query = new WP_Query( $qargs );

And now I can display 15 selected posts sorted by views like this:

if ( $posts_query->have_posts() ): ?>
                while ( $posts_query->have_posts() ):
                    $posts_query->the_post();
.......

But I need to select 15 most viewed posts and after that to sort them by date.

Is it possible?

Thank you!

1
  • This would need to be done using two queries.
    – Howdy_McGee
    Dec 9 '20 at 2:29
0

I found the way how to reorder already selected posts.

For exapmle, we have selected posts:

$qargs = array(
                'posts_per_page' => 15,
                'no_found_rows'  => true,
                'order' => 'DESC',
                'post_type' => 'post',
                'suppress_filters' => false,
                'orderby' => 'post_views', // here I order my posts by views
                'fields' => '',
                'date_query' => array(
                    array(
                        'after'     => '-7 days',
                        'column' => 'post_date',
                    ),
                ),
$posts_query = new WP_Query( $qargs );

Now we can reorder these posts by date:

function order_by_date( $a, $b ) {
        return strcasecmp( $b->post_date, $a->post_date );
    }
usort( $posts_query->posts, 'order_by_date' );

And next, we can echo our posts:

if ( $posts_query->have_posts() ): ?>
            while ( $posts_query->have_posts() ):
                $posts_query->the_post();
.......

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.