2
$args = array(
          'cat' => 5,
          'posts_per_page' => 8,
          'meta_key' => 'custom_sort_order',
          'order' => 'ASC',
          'orderby' => 'meta_value_num'
        );

$custom_query = new WP_Query( $args );

With the code above I can sort my posts with my custom field's value order, ascending. But if I forgot to assign the Custom Field key and value with the post, the query doesn't find the whole post because of the custom_sort_order meta_key — because it doesn't exist with that certain post.

But I need to put the 'meta_key' => 'custom_sort_order' there because the Codex says:

'meta_value_num' - Order by numeric meta value (available with Version 2.8). Also note that a 'meta_key=keyname' must also be present in the query.

Is there a way, I can by-pass it, and set a default sort order with the query but can manipulate them with my custom_sort_order meta_key with ascending numbers? So that, even if I forget to mention the custom_sort_order post_meta, the post will be queried as normal.

EDIT

After @heathenJesus's answer and @engelen's comment, it seems we are dealing the topic in a hard way. Can we think about a filter, by which we can modify a custom WP_Query() with our custom custom_sort_order meta_key?

Suppose our WP_Query arguments were:

 $args = array(
          'cat' => 5,
          'posts_per_page' => 8,
          'meta_key' => 'other_post_meta',
          'meta_value' => 'yes',
          'order' => 'ASC'
        );

$custom_query = new WP_Query( $args );

We excluded the 'orderby' => 'meta_value_num' and filtered our query with other meta_key and respective value. But custom_sort_order meta_key is not there. We're gonna implement some sort of filter and externally modify the order using our custom value custom_sort_order.

Can we think like that way?

Actually can we question: "How the Post Type Re-Order plugin did it that way?"

2
  • Or may be you can give some default value to custom_sort_order variable so that the post will always be there with your custom_query. Thought this would be a simple and nice alternate!!
    – sri
    Commented Jun 6, 2014 at 9:35
  • Unfortunately, there is no easy way to do this. The option @sri suggested is a good one, but you could also consider using custom SQL queries. If you're up to that, just let me know, I'll get you started with some examples and useful filters!
    – engelen
    Commented Jun 6, 2014 at 10:34

1 Answer 1

1

Short answer: You can't. Not like that, anyway.

You could, however, grab all of the results from your query, regardless of the presence of that custom field, and completely unsorted, and then sort them using the PHP function uksort https://www.php.net/manual/en/function.uksort.php

Something like:

$args = array(
      'cat' => 5,
      'numberposts' => 8
    );
$posts = get_posts($args);
uksort($posts,"sort_posts_by_custom_order");

function sort_posts_by_custom_order($a,$b)
{
  $custom_a = get_post_meta($a->ID,'custom_sort_order',true);
  $custom_b = get_post_meta($b->ID,'custom_sort_order',true);
  $custom_a = isset($custom_a) ? $custom_a : -1;
  $custom_b = isset($custom_b) ? $custom_b : -1;
  if ($custom_a == $custom_b) { return 0; }
  return ($custom_a < $custom_b) ? -1 : 1;      
}

Definitely a lot more expensive than making one DB call, but that's the price you pay for inconsistent data.

1
  • Thank you for your out-of-the-box thinking. Can you please see my edit in my question? Commented Jul 4, 2014 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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