[Gelöst] sorting an archive with custom post type and custom field
This support ticket is created vor 5 Jahre. 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.
Tell us what you are trying to do?
I have a custom post type of 'article' with custom field (numeric) of 'wpcf-numeric-sort'. I'm trying to follow how to add a function to my functions.php with the pre_get_posts action but the sort is having no effect (I see the results of the search but no sort is happening):
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() // Check that we are on the front end and not back end
&& !$q->is_main_query() // Make sure we only alter the main query
) {
return $query;
}
// only modify queries for 'article' post type
if( $query->is_archive() OR isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'article' ) {
echo "<h2>PHP is Fun!</h2>"; // just a check to see if my condition is working
Hello. Thank you for contacting the Toolset support.
Well - Can you please share problem URL on which archive you would like to apply the code you shared?
*** 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 have set the next reply to private which means only you and I have access to it.
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() // Check that we are on the front end and not back end
&& !$q->is_main_query() // Make sure we only alter the main query
) {
return $query;
}
// only modify queries for 'article' post type
if( $query->is_post_type_archive('article')) {
//echo "<h2>PHP is Fun!</h2>"; // just a check to see if my condition is working
$query-> set('order', 'DESC');
$query-> set('meta_key', 'wpcf-numeric-sort');
$query-> set('orderby', 'meta_value_num');
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts' );
When I check the field values for "Numeric Sort" in backend and compare the result it display on frontend, I see not the numeric sort order is working as expected.
=> hidden link
I'm still not seeing it working as expected. I will try to create an archive which shows the actual field wpcf-numeric-sort which it should be sorting by so we can more easily trouble shoot.
I see you changed the condition for the loop, but left the 3 $query statements as I had them. So I guess you liked the logic and also don't know why it is not working?
It seems that the Toolset archive has a sort option which I didn't notice before and was probably overriding the custom function I was trying to write. So once I selected in the Toolset archive the numeric-sort field that I wanted to sort by all was good. Very nice simple feature which I overlooked. My bad, sorry.
My issue is resolved now. Thank you!
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.