The forums are closed and will be removed when we launch our new site.
Looking for support? You can access the support system via your account.
Forums › Forums › Search & Filter Pro › modify $query
- This topic has 14 replies, 3 voices, and was last updated 5 years, 10 months ago by Ross.
-
Anja Faeßer(Private) October 17, 2018 at 3:22 pm #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 Moderator(Private) October 17, 2018 at 3:36 pm #191183I 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?
Trevor Moderator(Private) October 18, 2018 at 12:56 pm #191241This 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?
Anja Faeßer(Private) October 18, 2018 at 1:18 pm #191245Yes, 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, II’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 Moderator(Private) October 18, 2018 at 1:48 pm #191250It 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.
Anja Faeßer(Private) October 18, 2018 at 2:19 pm #191253That 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 Moderator(Private) October 18, 2018 at 2:46 pm #191255It is the only solution I can think of.
Anja Faeßer(Private) October 18, 2018 at 6:49 pm #191286Now 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 Moderator(Private) October 18, 2018 at 7:30 pm #191290It 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?
-
AuthorPosts
The forum ‘Search & Filter Pro’ is closed to new topics and replies.