• So I would like to arrange my category page posts by the order of a Metafield date rather than the normal date published. I have researched and tried some of the methods mentioned on other forums without any luck.

    $args = array(
        'meta_key' => 'expiry',
        'orderby'  => 'meta_value_num date'
    );
    
    $query = new WP_Query( $args );

    Is what I tried… with lots of variations. It DID work on one category but not all of them, now I am lost…

    If anyone has some guidance how to do so, the metafield value is in DATETIME format ‘2017-07-05 23:00:00’.

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Have you tried adding 'meta_type' => 'DATETIME',?

    I’m not sure that would help, what you you’ve done appears OK. What you should do is echo out $query->query after instantiating $query. This is the SQL string that was run to get your results. Whatever is wrong would be evident here.

    WP query strings always have a WHERE 1=1 clause, which seems silly. It’s not. It allows the query to run if there are no subsequent WHERE clauses added. When WP cannot parse the supplied arguments correctly, it will place WHERE 1=0 in the query so it will fail no matter what. If you should see this, there is a problem with the query arguments somewhere. Otherwise, the problem will be evident in some other part of the SQL string. Identify the SQL problem, which should point back to what is wrong with the query arguments.

    The problem may not be anything you did. There are several filters plugins and themes can hook to alter queries. Any of them could be inappropriately applying their change to your query. This will show up in the SQL string. If the guilty component is not evident from the SQL issue, selectively deactivate suspect components until the problem goes away.

    Thread Starter dealdaddy

    (@dealdaddy)

    Thanks for your response.

    So far it is looking like this;

    $args = array(
    'meta_key' => 'expiry',
    'meta_type' => 'DATETIME',   
    'orderby'  => 'meta_value_num date'
    );
    
    $query = new WP_Query( $args );
    
    print_r($query->query);

    I tried echoing the SQL but it returns an array..

    Array ( [meta_key] => expiry [meta_type] => DATETIME [orderby] => meta_value_num [numberposts] => 50 ) ?

    I added in numberposts to make sure that it wasn’t an infiniteloader plugin which would evidently load X more posts and not all of them…

    Thread Starter dealdaddy

    (@dealdaddy)

    SELECT SQL_CALC_FOUND_ROWS oi7m_posts.ID FROM oi7m_posts INNER JOIN oi7m_postmeta ON ( oi7m_posts.ID = oi7m_postmeta.post_id ) WHERE 1=1 AND ( oi7m_postmeta.meta_key = 'expiry' ) AND oi7m_posts.post_type = 'post' AND (oi7m_posts.post_status = 'publish') GROUP BY oi7m_posts.ID ORDER BY oi7m_postmeta.meta_value+0 DESC LIMIT 0, 10

    Oh i got it.. does that make any sense?

    Moderator bcworkz

    (@bcworkz)

    I’m glad you figured out I meant $query->request. Sorry about that.

    Yes, the request appears to be in order. I don’t know why the Codex suggested meta_type, it is not part of the resulting request and I don’t see where it is considered in source code. No matter, ordering the date format you are using as meta_value_num should be just fine. (FYI, adding +0 to an order by field is an old SQL trick to force numeric evaluation).

    It’s still possible for a plugin or theme conflict to be causing problems even if the request is correct. Something is altering the request, though the result should still work for you. It is missing the usual default alternate post statuses like pending, draft, etc. The secondary date orderby argument is also missing.

    Try adding this in place of the echo request line:
    print_r($query->posts);
    You should get a long listing of post object data for posts found by the request. If you get an empty array, no posts were found. Assuming posts were found here but no output appears on your page, some plugin or your theme is messing with your looping of the results. Try selectively deactivating suspect components until the problem goes away.

    If you get an empty array, there’s something wrong with your query assumptions and there really are no matching posts. Review each = clause in the request and confirm it’s correct. Skip the first one after ON, I know that one is correct. Be sure the meta key on the post edit screens is really “expiry” and not “Expiry” or anything else that is not an exact match.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Sort by Meta Field Date’ is closed to new replies.