I'm trying to customize the post order of a wordpress-archive using a custom field called "post-order".
I added the following code to functions.php
function sort_products_archive_loop($query) {
if (is_tax('product_cat') && $query->is_main_query()) {
$query->set('order', 'DESC');
$query->set('meta_key', 'wpcf-product-order');
$query->set('orderby', 'meta_value_num');
}
}
add_action('pre_get_posts', 'sort_products_archive_loop');
The only problem i am now facing is that products are not visible when this field is empty.. Is there a way to work around this? By having products always visible even if "post-order" is empty... the order does not matter..
Hello. Thank you for contacting the Toolset support.
Well - what you are getting is expected results as if there is no field value available those entries will be discarded. So, you should set a default value to 0 to your all products to the custom field that holds the ordering value.
What you mean by filtering a query? Well - I do not have any example to offer you as it needs custom programming that is eventually beyond the scope of our support policy.
You may try to search by term "set default values for custom field wordpress" in google.
This is one of the example - not exactly you need but you may get idea. In addition to that please backup your site before you run any custom query to be at safer side:
=> hidden link