$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?"
custom_sort_order
variable so that the post will always be there with yourcustom_query
. Thought this would be a simple and nice alternate!!