[Resolved] Sort Custom Post Archive by Custom Field and show products even if field isempty
This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
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