Skip Navigation

[Resolved] Sort Custom Post Archive by Custom Field and show products even if field isempty

This support ticket is created 5 years, 10 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by troyC 5 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1200892

Hello,

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..

#1200959

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

Either you should fire a query or do it manually.

#1201683

Oke thank you for your response....

Can you help with "firing a query" by giving an example or some terms i can google to research this?

#1201728

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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

#1201779

My issue is resolved now. Thank you!