Skip Navigation

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

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.

This topic contains 11 respuestas, has 3 mensajes.

Last updated by jasonH-4 hace 5 a帽os, 11 meses.

Assigned support staff: Juan.

Autor
Mensajes
#247617

Hi there,

I already got help on this thread (https://toolset.com/forums/topic/allow-user-to-choose-sort-order-and-get-it-to-work-with-pagination/) 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:

Aardvark
Bears
The Cat
Dogs
A Fox
Giraffe
etc.

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! 馃檪

#247754

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.

Regards
Caridad

#247986

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

#249818

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?

Thanks
Caridad

#249836

Yes to both. Thanks.

#249837

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! 馃檪

#250326

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

#250440

Juan
Supporter

Timezone: Europe/Madrid (GMT+02: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?

#251042

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

The types name for the custom field is:

wpcf-alpha-list

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.

THANK YOU!!! 馃檪

#251446

Juan
Supporter

Timezone: Europe/Madrid (GMT+02: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 馃檪

Regards,
Juan de Paco

#251514

Perfection! That is so great. Love you guys!

#251515

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