Forums Forums Search & Filter Pro modify $query

  • This topic has 14 replies, 3 voices, and was last updated 6 years ago by Ross.
Viewing 10 posts - 1 through 10 (of 15 total)
  • Anonymous
    #191180

    I would like the results of a term of this taxonomy to always be displayed when selecting a term from my own taxonomy.

    Example:
    Taxonomy: Regions
    Terms: Austria, Vienna, Salzuburg etc.
    Articles with the term “Austria” should always be displayed if one of the other terms is selected.

    For the taxonomy archive page I have added the following to the “functions.php”:

    function my_modify_main_query( $query ) {
    	if ( !is_admin() && $query->is_main_query() ) {
    		
    			$query->set( 'tax_query', array(
    				'relation' => 'OR',
    				array(
    					'taxonomy' => 'region',
    					'field' => 'slug',
    					'terms' => 'Österreich'
    				),
    			) );
    		
    	}
    }
    add_action( 'pre_get_posts', 'my_modify_main_query' );

    Unfortunately this does not affect the result of “Search & Filter”. I use the shortcode method for display and in the file “results.php” the element “$query” is actually addressed as well.

    Any approach on how to get my behavior realized?

    Trevor
    #191183

    I am not sure this is the way to achieve your aim, although I am not entirely sure what you ware trying to do.

    Are you able to send me a live link/URL to your search page so I can take a look?

    Can you suggest a search for me to make, and detail what does happen (what posts appear), and what you want to see?

    Anonymous
    #191200
    This reply has been marked as private.
    Trevor
    #191241

    This site is about Austria only, no other country? As you have Austria in regions, does this mean that when Austria is selected it actually means all regions of Austria?

    Anonymous
    #191245

    Yes, when I choose Austria the article refers to the whole country.
    If I select a single region, the articles that are tagged for the whole of Austria should also be displayed.

    Perhaps a more detailed explanation. I have the following articles.

    Article A, B, C with tag “Austria”
    Article D, E, F with tag “Vienna”
    Article G, H, I with tag “Tyrol”

    Tag Selection “Austria” shows the articles: A, B, C
    Tag Selection “Vienna” shows the articles: A, B, C, D, E, F
    Tag Selection “Tyrol” shows the articles: A, B, C, G, H, I

    I’ve already customized the output of “$query” (see first post) via the “functions.php and that works for the tag archive page of the regions. Unfortunately it doesn’t affect the results of “search & Filter Pro”.

    Trevor
    #191250

    It would be simpler, but I am not sure if it meets your needs, not to have the Austria tag but instead for those articles to select all region terms. That way no special coding is needed to do what you want.

    Anonymous
    #191253

    That would now be the dirty solution and error-prone. Would then also increase the effort when creating the articles.

    If there is no elegant solution for give from your side I must probably live with.

    Trevor
    #191255

    It is the only solution I can think of.

    Anonymous
    #191286

    Now I thought about the problem again and looked at the documentation. I found the following: “Edit Query Arguments”.

    Then I tried the following:

    function filter_function_name( $query_args, $sfid ) {
    
    	if($sfid==163) {
    
    		$query_args['tax_query'] = array(
    			'relation' => 'OR',
    			array(
    				'taxonomy' => 'region',
    				'field'    => 'slug',
    				'terms'    => array( 'Österreich' ),
    			),
    		);
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    Unfortunately this works exactly the other way round to what I want.
    Now only articles with the tag Austria are displayed.

    Is there any way to modify it to work the way I think it should?

    Trevor
    #191290

    It may be that it is replacing not adding to the query. You could extract the term already in the query and add it to that?

Viewing 10 posts - 1 through 10 (of 15 total)