Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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".

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.

share|improve this question
    
This doesn't make sense to me: "When user is refreshing his premium offer, it's 'post_modified' is changed." What does that mean exactly in context? How are your offers implemented? And, for the record, I am betting that the problem isn't your query, it is your data structure. –  s_ha_dum Apr 11 at 13:14
    
when you fires wp_update_post the post_modified is changed to actual time. so when user click on 'refresh' link i make update post hook. Users add new offers with form in their account. What do you mean that my data structure is failure? –  Kamil Apr 12 at 8:33
    
It is very difficult to tell what you are doing but it it sounds like the way that you have chosen to organize your data makes the query you want difficult. –  s_ha_dum Apr 12 at 8:59
    
so maybe you can help me with reorganize structure for this kind of problem. very important is separation of posts to "free" and "premium" so that premium posts must be shown before the rest, non premium. refreshing is also very important. any clue? it's very important for me to figured out asap. –  Kamil Apr 12 at 17:21
    
any solution to make this happen? order posts by type (premium or free) and let user modified the date of his posts. than the latest modified posts with premium type is on top of the list. Please help! –  Kamil Apr 16 at 10:07

1 Answer 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.