I order my posts by custom field faq-order. That's work for me:
$faqArgs = array(
'post_type' => 'faq',
'meta_key' => 'faq-order',
'order' => 'ASC',
'orderby' => 'meta_value_num',
);
$faq = new WP_Query($faqArgs);
But post disappear from result response if those field faq-order is empty. How can I fix it? How can I use default value (for example 0), if that field is empty?
I order my posts by custom field faq-order. That's work for me:
$faqArgs = array(
'post_type' => 'faq',
'meta_key' => 'faq-order',
'order' => 'ASC',
'orderby' => 'meta_value_num',
);
$faq = new WP_Query($faqArgs);
But post disappear from result response if those field faq-order is empty. How can I fix it? How can I use default value (for example 0), if that field is empty?
There are two solutions for that:
You can do this easily - just use save_post
hook and set it to default value. (You should also add default values for already existing posts that don't have that field set.)
$faqArgs = array(
'post_type' => 'faq',
'meta_key' => 'faq-order',
'order' => 'ASC',
'orderby' => 'meta_value_num',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'faq-order',
'value' => 'bug #23268',
'compare' => 'NOT EXISTS'
)
)
);
$faq = new WP_Query($faqArgs);