I have a WordPress custom query:
$args = array(
'posts_per_page' => 2,
'meta_key' => 'pworks_popular_posts',
'paged' => $page_nr
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
$pworks_post_meta = get_post_meta(get_the_ID(), 'pworks_popular_posts', true);
$pworks_post_list[get_the_ID()] = $pworks_post_meta['Post visited'];
}
}
I am using this method to sort an array of post ids and post visits which then I sort using arsort
and display a list of posts by the number of visits they got:
arsort($pworks_post_list);
However, I realized that pagination will be a nightmare if I continue to do it this way, as there is no way to go to a specific page of the sorted results, only the unsorted ones.
To accomplish this I have to order my posts by meta_value and ditch the arsort()
method.
The problem is - the meta value is an array that looks like this (I did it this way because I will most likely be adding more fields to that array later):
array(1) {
["Post visited"]=>
int(0)
}
Is there a way to order the query by this "Post visited" array field in WP_Query?
I would appreciate your help greatly.