Custom Post Type order by modified date and custom meta field issue

I’m creating a plugin with some kind of offers that users can publish. User, in his custom administration panel, can managed his own offers. Offers are free and premium. If offer is premium it take place above the free offers in whole list. User can refresh premium offers daily so offer that has been refresh is landing at the top of the list.

I figured it out like this:
offers has custom fields "premium" and if offer is “premium” the value is “2”, otherwise (free offer) the value is “1”.

Read More

When user is refreshing his premium offer, it’s 'post_modified' is changed.

What kind of query do I have to made to make this happen:
newest (with latest modified date) premium offers (with custom meta “premium” equal “2”) on top of the list and other offers below it.

My actual query looks like this:

$args = array(
    'post_type' => 'mycustomposttype',
    'post_status' => 'publish',
    'paged' => $paged,
    'meta_key' => 'premium',
    'orderby' => 'modified meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
        array(
        'key' => 'paid',
        'value' => array('paid','free'),
        'compare' => 'IN'
        )
    ),
);

but the newest offer is in the end of list. even when i changed order to ‘DESC’.
Any help (or new way to do this) will be much appreciate.

Related posts

Leave a Reply

1 comment

  1. If I understand you, I believe that the following will do what you need:

    function orderby_mod_wpse_140999($orderby) {
      remove_action('posts_orderby','orderby_mod_wpse_140999');
      global $wpdb;
      return $orderby.", {$wpdb->posts}.post_modified DESC";
    }
    add_filter('posts_orderby','orderby_mod_wpse_140999');
    
    $args = array(
      'meta_key' => 'premium',
      'orderby' => 'meta_value',
      'order' => 'ASC'
    );
    $q = new WP_Query($args);
    var_dump($q->request);
    

    The key names may be wrong and it may be (probably is) better rewritten as a pre_get_posts action, something like:

    function orderby_mod_wpse_140999($orderby) {
      remove_action('posts_orderby','orderby_mod_wpse_140999');
      global $wpdb;
      return $orderby.", {$wpdb->posts}.post_modified DESC";
    }
    add_filter('posts_orderby','orderby_mod_wpse_140999');
    
    function pgp_orderby_mod_wpse_140999($qry) {
      if ($qry->is_main_query() && is_archive()) {
        $qry->set('meta_key','premium');
        $qry->set('orderby','meta_value');
        $qry->set('order','ASC');
      }
    }
    add_action('pre_get_posts','pgp_orderby_mod_wpse_140999');
    

    Again, I am guessing a bit. For the pre_get_posts solution the code would need to go somewhere that will allow the hook to apply to the main query.