I created like system in wordpress. And I created 'sayim_beyenme' table and created 'say_post_id' and 'total_post_likes' datas. Now I want to display my posts orderby total likes per each post. I get posts ids with array_map, but I dont know how can I order these posts by total_post_like values. For testing I have used 'title' This is my codes:
<?php
$results = $db->prepare("SELECT say_post_id FROM sayim_beyenme ORDER BY total_post_likes DESC LIMIT 3");
$results->execute();
$results = $results->fetchAll();
$like_posts = array_map(function(&$r) { return $r['say_post_id']; }, $results);
if($like_posts) {
$postlar = new WP_Query(array(
'paged' => get_query_var('paged', 1),
'post_type' => array('post'),
'posts_per_page' => 9,
'post__in' => $like_posts,
'orderby' => 'title',
'order' => 'ASC',
));
I created like system in wordpress. And I created 'sayim_beyenme' table and created 'say_post_id' and 'total_post_likes' datas. Now I want to display my posts orderby total likes per each post. I get posts ids with array_map, but I dont know how can I order these posts by total_post_like values. For testing I have used 'title' This is my codes:
<?php
$results = $db->prepare("SELECT say_post_id FROM sayim_beyenme ORDER BY total_post_likes DESC LIMIT 3");
$results->execute();
$results = $results->fetchAll();
$like_posts = array_map(function(&$r) { return $r['say_post_id']; }, $results);
if($like_posts) {
$postlar = new WP_Query(array(
'paged' => get_query_var('paged', 1),
'post_type' => array('post'),
'posts_per_page' => 9,
'post__in' => $like_posts,
'orderby' => 'title',
'order' => 'ASC',
));
Share
Improve this question
asked Mar 31, 2020 at 8:31
Cavid MuradovCavid Muradov
215 bronze badges
2 Answers
Reset to default 0I found my answer, I added 'orderby' => 'post__in'
You can't directly, instead you'll need to figure that out yourself then provide the sort order manually to WP_Query
and specify post__in
as the orderby
value