Skip Navigation

[Resuelto] Allow User to choose sort order (using custom field)

This support ticket is created hace 8 años, 4 meses. 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
- 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Europe/Madrid (GMT+01:00)

This topic contains 11 respuestas, has 3 mensajes.

Last updated by jasonH-4 hace 8 años, 3 meses.

Assigned support staff: Juan.


Hi there,

I already got help on this thread ( in terms of allowing a user to sort using post title or date and have it work with pagination, and now I am looking to get the same thing to work with a custom field instead of the post title.

I confess I'm not sure at all how to set this up in terms of what to add to functions.php.

The only reason I am doing this is because I want to be able to sort my list so all the titles that star with "the" and "a" don't get lumped together.

Such as:

The Cat
A Fox

I figure I can make a custom field that is slightly different than the post title (which will have "the" and "a" in it) so it can sort the way I want.

Maybe there is a better way to do this but that is my idea.

Again, my goal here is to learn the code to use to have a user sort by a custom field (and have it work with pagination).

Thanks! 🙂


Dear Jason

Can you share access so I can see how far you got and what is missing?

You can use the boxes below the comment area, after you agree to the disclaimer.



I haven't gotten very far! I have set up the custom field "Alpha List" but don't know how to write the code for functions.php or the combo output for the view. If this is too much to ask let me know. Not a must have but definitely would be nice to have!

hidden link


We should be able to do everything through the Dashboard with no custom code.

I have a few questions before I start:
Do you have a View created already?
Did you also create a page to display this view?



Yes to both. Thanks.


Also wanted to add that Juan did at some custom code to my functions.php in the other thread where I had things sorted by title (because my view uses pagination). Curious to see this work without custom code! 🙂


Thanks for the notice, I have asked Juan to take over this ticket.



Timezone: Europe/Madrid (GMT+01:00)

Hi Jason!

This is Juan, glad that I could help you before, and willing to do it again 🙂

I remember I edited come custom code so that you could display links that set the sorting of the View manually and played nicely with pagination. I understand that what you want now is to display an additional link that will sort the View by a custom field value (and you are using this custom field to store the post title without some starting kind-of-prefix words).

To do so, I will need the slug of the custom field you are using to store that kind-of titles. After that, I will update the code I added to your site so you can create that sorting link. THen, I will add that code here so anyone else can benefit from it. Maybe it wil be useful for me when we implement a canonical way of doing so 🙂

How does it sound?


Hi Juan, that is a fantastic, generous offer. Thank you so much!

The types name for the custom field is:


I assume this is the slug? I don't see anything that's listed as a slug for a Custom Field.

In any case, it is the ONLY custom field with my setup.




Timezone: Europe/Madrid (GMT+01:00)

Hi Jason

Sorry for the late reply, but I have good news 🙂 I did not need to add any new code to your site. I just needed to pass the right atribute to the shortcode we created before. To sort by a custom field with slug wpcf-alpha-list, you just need to pass the attribute sort="post-field-wpcf-alpha-list".

I added this to your Comics page and it seems to be working fine. If you could confirm it, we could close this ticket 🙂

Juan de Paco


Perfection! That is so great. Love you guys!


And here's the code in question in case it helps anyone! 🙂

I think I have it right. substitute your own codename wherever it says "create_your_codename_here". And this goes in functions.php.

add_shortcode('create_your_codename_here, 'create_your_codename_here_shortcode');
function create_your_codename_here_shortcode($atts) {
	extract( shortcode_atts( array(
         'sort' => 'date',
         'dir' => 'DESC',
         'numeric' => '',
    ), $atts ) );
	global $WP_Views;
	$args = array(
		'wpv_column_sort_id' => $atts['sort'],
		'wpv_column_sort_dir' => $atts['dir']
	if ( isset( $WP_Views ) ) {
		$args['wpv_view_count'] = $WP_Views->get_view_count();
	$url = $_SERVER['REQUEST_URI'];
	if ( strpos( $url, 'wpv-ajax-pagination' ) !== false || ( defined('DOING_AJAX') && DOING_AJAX ) ) {
		$base_url = wp_get_referer();
		return add_query_arg( $args, $base_url );
	} else {
		return add_query_arg( $args );