Skip Navigation

[Résolu] URL parameter not working as expected

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I have a taxonomy View that responds to a URL parameter, and should filter only terms that begin with that letter. In the past, this View was working correctly but it has begun to show inaccurate results.

Solution: Replace your taxonomy query filter code with the updated version below.

add_filter( 'wpv_filter_taxonomy_query', 'prefix_modify_tax_query', 10, 3 );
function prefix_modify_tax_query( $tax_query_settings, $view_settings, $view_id ) {
 
    if($view_id != 62 && isset($_GET['wpvalphabet']) && $_GET['wpvalphabet'] != '' ) {
        $tax_query_settings['name__like'] = $_GET['wpvalphabet'];
    }
    return $tax_query_settings;
}
function old_style_name_like_wpse_123298($clauses) {
      remove_filter('term_clauses','old_style_name_like_wpse_123298');
    $splits = explode('}', $clauses['where']);
    $joins = array();
    if(sizeof($splits) == 1) {
      return $clauses;
    }
    foreach($splits as $split) {
      $joins[] = substr($split, 0, strpos($split, '{'));
    }
    $clauses['where'] = join($joins, '') . (sizeof($joins) > 1 ? "%'" : '');
      return $clauses;
}
 
add_filter('terms_clauses','old_style_name_like_wpse_123298');

Relevant Documentation: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_query
https://developer.wordpress.org/reference/hooks/terms_clauses/

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.

This topic contains 4 réponses, has 2 voix.

Last updated by morktron Il y a 3 années et 11 mois.

Assigned support staff: Christian Cox.

Auteur
Publications
#584769
odd-q.png

Hi, the main Views page of my site is suddenly behaving oddly. When you click in the Alphabet terms the Subject terms should be displayed, instead, it is randomly saying 'No items found'.

These are the original posts about setting the site up:

https://toolset.com/forums/topic/stop-rest-of-alphabet-pagination-disappearing-when-clicking-on-pagination/

https://toolset.com/forums/topic/a-z-navigation-of-contemplations-cpt-grouped-by-subject-taxonomy/

It was all working perfectly and I haven't changed anything that could affect the main View page - as far as I can think of.

I duplicated the site and disabled all plugins expect Toolset, changed to the default 2017 theme but still the same issue. I tried disabling the Views cache but still the same.

Can you think of a reason why it is not working? thanks

#584939

Hi, is it okay for me to create a clone of your site to run some tests locally? I'd like to see what's happening with the filter since it was working before. If it's okay, I will install the Duplicator plugin and begin testing.

#585007

Hi Christian, sure that would be fine, thanks

#585498

Thanks, I was able to replace the previous code with the following update:

add_filter( 'wpv_filter_taxonomy_query', 'prefix_modify_tax_query', 10, 3 );
function prefix_modify_tax_query( $tax_query_settings, $view_settings, $view_id ) {

	if($view_id != 62 && isset($_GET['wpvalphabet']) && $_GET['wpvalphabet'] != '' ) {
		$tax_query_settings['name__like'] = $_GET['wpvalphabet'];
	}
    return $tax_query_settings;
}
function old_style_name_like_wpse_123298($clauses) {
	  remove_filter('term_clauses','old_style_name_like_wpse_123298');
    $splits = explode('}', $clauses['where']);
    $joins = array();
    if(sizeof($splits) == 1) {
      return $clauses;
    }
    foreach($splits as $split) {
      $joins[] = substr($split, 0, strpos($split, '{'));
    }
    $clauses['where'] = join($joins, '') . (sizeof($joins) > 1 ? "%'" : '');
	  return $clauses;
}

add_filter('terms_clauses','old_style_name_like_wpse_123298');

Please make a full backup of your functions.php file before you try this change, and let me know if it's not working 100% as expected.

#585617

Wow thanks Christian! that has worked 🙂

So it looks like some of the Subject names where causing issues with the URL parameter because they had spaces, dashes and so on that don't work in URLS. I'm just guessing because my PHP skills are very basic!

Thanks so much Christian 🙂