最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Set WP_query 'order' option by another tables value

programmeradmin3浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 0

I 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

发布评论

评论列表(0)

  1. 暂无评论