Set WP_query ‘order’ option by another tables value

Question

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',
                    ));
0
, Cavid Muradov 3 years 2020-03-31T04:51:24-05:00 0 Answers 88 views 0

Leave an answer

Browse
Browse