Forums

Sort by custom field, or lack thereof (3 posts)

  1. Oolong
    Member
    Posted 7 months ago #

    Hi, I've found lots of posts about sorting by the value of a custom field, most of which miss the fact that the wp_query class now makes this a bit easier using meta_value_num - but I haven't been able to find anything to sort by a meta_value without excluding all posts lacking the relevant meta_key. What I want is a page which lists posts with a 'priority' value set first, but includes those without. All the ways I've seen of sorting my the value of a custom field rely on directly or indirectly calling SQL with the equivalent of 'WHERE meta_key=priority' so that it knows which key's value to use.

    What I have at the moment is this (adapted from the PageOfPosts template found as an example here on WordPress.org):

    $args=array(
        'category__in' => array($cat),
        'meta_key' => 'priority',
        'orderby' => 'meta_value_num, date',
        'order' => 'DESC',
        'paged' => $paged,
        'posts_per_page' => $post_per_page,
        'caller_get_posts' => $do_not_show_stickies
      );
      $temp = $wp_query;  // assign orginal query to temp variable for later use
      $wp_query = null;
      $wp_query = new WP_Query($args);

    I guess I could just call this twice, once with the above and once excluding posts with the relevant custom field, but that seems awfully kludgey. Is there a neater way?

  2. keesiemeijer
    moderator
    Posted 7 months ago #

  3. Oolong
    Member
    Posted 7 months ago #

    Thanks... the first link gives some specifics on how I could exclude posts with this custom field from my query, which does take me to a working answer, but a kludgey one, as I said. And meta_query is nice - surprised I'd missed that - but I'm not seeing a way to use it to solve this problem with reasonably elegant code. Actually, I'm not seeing a way to use it here at all, which makes me wonder if I'm just being dense.

Reply

You must log in to post.

About this Topic