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

wp query - SELECT TOP 1 in wp_query for each groupby meta value

programmeradmin1浏览0评论

Is there any way using wp_query class to execute SELECT TOP 1 sql command while using groupby for each meta_value results. I need the wp_query to select only the first one of each groupby meta_value results. Cant find on google any example with wp_query! I can only find sql queries using get_posts wordpress function but i dont want to use get_posts. Any help appreciated.

Thanks

Is there any way using wp_query class to execute SELECT TOP 1 sql command while using groupby for each meta_value results. I need the wp_query to select only the first one of each groupby meta_value results. Cant find on google any example with wp_query! I can only find sql queries using get_posts wordpress function but i dont want to use get_posts. Any help appreciated.

Thanks

Share Improve this question asked Nov 5, 2019 at 2:38 stefanosnstefanosn 1339 bronze badges 2
  • What are you grouping exactly? WP_Query doesn’t support that type of query. – Jacob Peattie Commented Nov 5, 2019 at 4:12
  • grouping posts by a meta value but it always groups and shows me the first inserted post based on date order of each one grouped value. I need somehow to change the query to make it show the last inserted post (most recent) for each grouped value. Is this somehow possible? I use posts_groupby filter to group by a meta_value i have in my database. – stefanosn Commented Nov 5, 2019 at 10:16
Add a comment  | 

1 Answer 1

Reset to default 0

You could execute a query for each meta group to retreive the id of each category latest post and store the ids in an array. Then cicle trough the array as if you were in the loop.

There might be a better way, but this is what i came up with at the moment of writing.

<?php
// Please note the following code is not tested and might contain sintax errors

// define result array and categories to search
$latest_post_from_each_category = [];
$categories = [
    [
        'key' => 'meta_key_1',
        'value' => 'meta_value_1'
    ],
    [
        'key' => 'meta_key_2',
        'value' => 'meta_value_2'
    ]
];

// Cicle trough categories to get each cat's latest post ID
foreach ($categories as $cat) {

    $args = [
        'post_type' => 'post',
        'post_status' => 'publish',
        'fields' => 'ids',              // The query will return an Array of IDS
        'posts_per_page' => '1',        // Only get one post for each cat
        'order_by' => 'date',
        'order' => 'DESC',
        'meta_query' => [
            [
                'key'     => $cat['key'],
                'value'   => $cat['value'],
            ],
        ],
    ];

    $query = new WP_Query( $args ); // This will return an array containing a single integer (or an empty array if no post has been found)
    $latest_post_from_each_category = array_merge($latest_post_from_each_category, $query); // Merge the orignal array with the new id found
};

var_dump($latest_post_from_each_category); // Array of ids you need

// just cicle trough the array and get the info ou need by the post id

发布评论

评论列表(0)

  1. 暂无评论