Skip Navigation

[Resolved] sorting an archive with custom post type and custom field

This support ticket is created 5 years 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)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by martinE-4 5 years ago.

Assisted by: Minesh.

Author
Posts
#1233500

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

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

Is there any documentation that you are following?

https://toolset.com/forums/topic/sort-custom-post-archive-by-custom-field/

Is there a similar example that we can see?

What is the link to your site?
hidden link

#1233663

Minesh
Supporter

Languages: English (English )

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

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.

#1234378

Minesh
Supporter

Languages: English (English )

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

Well - I've adjusted the code as given under:


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

#1234556

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?

Thanks so much for looking at this.

#1234646

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.