简体   繁体   English

WP_Query按自定义元值排序,然后按修改顺序排序

[英]WP_Query order by custom meta value and then order by modified

I'm trying to order a wp_query that includes a meta_query ... 我想订购wp_query包括meta_query ...
After some different approaches, it seems that the query can't be ordered since I include the meta_query , and orderby => ID is used by default (I think) and it can't be overwritten. 在一些不同的方法之后,似乎无法对查询进行排序,因为我包含了meta_query ,并且默认使用了orderby => ID (我认为)并且它不能被覆盖。 Any ideas on how to accomplish this? 有关如何实现这一目标的任何想法?
Here's the PHP code I use just to display the posts by meta value: 这是我用来按元值显示帖子的PHP代码:

$paged = get_query_var('paged') ? get_query_var('paged') : (get_query_var('page') ? get_query_var('page') : 1);
$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'paged' => $paged,
    'meta_query' => array(
        array(
            'key' => 'subtitrare',
            'value' => 'romana',
            'compare' => 'LIKE'
        )
    )
);

$listing_query = null;
$listing_query = new WP_Query($args);
if ($listing_query->have_posts()) : get_template_part('loop-item');
endif;
wp_reset_postdata();

As you can see I also paginate the results, so i can use the same $args . 正如你所看到的,我也对结果进行了分页,所以我可以使用相同的$args I've tried to use a custom function to rearrange the results but it didn't worked for me. 我试图使用自定义函数来重新排列结果,但它对我没有用。

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'paged' => $paged,
    'orderby' => 'modified',
    'order' => 'DESC',
    'meta_query' => array(
        array(
            'key' => 'subtitrare',
            'value' => 'romana',
            'compare' => 'LIKE'
        )
    )
);

This is the code you need but you already said that you tried this. 这是你需要的代码,但你已经说过你试过了。 This will return all the posts which have the custom field value set to LIKE 'romana'. 这将返回自定义字段值设置为LIKE'romana'的所有帖子。 What are the results you get from using this code? 使用此代码可获得哪些结果?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM