Skip Navigation

[Resolved] Sort Custom Post Archive by Custom Field 2.0

This thread is resolved. Here is a description of the problem and solution.

Problem:
Sort Custom Post Archive by Custom Field 2.0

Solution:
You can use WordPress hook "pre_get_posts" in order to sort post type archive using custom field value.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/sort-custom-post-archive-by-custom-field-2-0/#post-921727

Relevant Documentation:

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

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 6 replies, has 2 voices.

Last updated by troyC 6 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#919179

I am trying to change the default order of a archive to be set by custom-field. I thought https://toolset.com/forums/topic/sort-custom-post-archive-by-custom-field/ would save my life, but it did not yet.

I tried changing code to fit my needs.

The custom post type is from woocommerce called 'product'
The numerical custom field slug is: product-order

 

function sort_products_archive_loop($query) { 
    if ($query->is_post_type_archive('product') && $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');

Link to a page where the issue can be seen:
hidden link

Help is appreciated !!

#919697

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - your code looks like OK but without access details I will not be able to tell you whats causing the issue or why the order is not working.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#921727

Minesh
Supporter

Languages: English (English )

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

Could you please check now. I've adjusted the code in your current theme's functions.php file as given under:

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 issue was you were checking with post type archive but the current page you are loading is taxonomy archive so you need to check using the function is_tax().

More info:
=> https://codex.wordpress.org/Function_Reference/is_tax

I see its working fine - could you please confirm.

#922385

Yes it seems to work a little bit better now. But now I get different problem... products are now missing from the categories.

#922386

Minesh
Supporter

Languages: English (English )

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

Well - I just noticed that you are using Beta plugins, those are outdated and we have stable release available for all Toolset plugins.

We always recommend to run your site with latest stable release plugin version.

*** Please make a FULL BACKUP of your database and website.***
Could you please update ALL Toolset plugins to it's latest official released version. You can download latest plugin release from your accounts page:
=> https://toolset.com/account/downloads/

#956375

Thank you for your help so far Minesh.

I have updated the plugins views and types but the problem still occurs. I also can not add pagnation to the wordpress archive. Products are limited to a maximum of three

Any suggestions?

#956423

Sorry... I CAN add pagination... but the problem of max 3 products still occurs with the code activated:

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');