简体   繁体   中英

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 ...
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. Any ideas on how to accomplish this?
Here's the PHP code I use just to display the posts by meta value:

$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 . 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'. What are the results you get from using this code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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