Skip Navigation

[Resolved] Display all posts from 1 taxonomy.

This thread is resolved. Here is a description of the problem and solution.

Problem:
Display all posts from 1 taxonomy. - prefilter view results by filtering view results with taxonomy terms

Solution:
You should try to use the hook "wpv_filter_query" to pre-filter your view results by specific terms.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/display-all-posts-from-1-taxonomy/#post-1227954

Relevant Documentation:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 5 years, 8 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 17 replies, has 2 voices.

Last updated by lewisP 5 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#1227186

Hi, I have CPT called Members, with 3 taxonomies. I am trying to create a view to display and filter 1 taxonomy. Please can you help me.

#1227236

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - sure. To filter your view by taxonomy, you need to add the taxonomy filter.

Please check the following document for more information:
=> https://toolset.com/documentation/user-guides/filtering-views-by-taxonomy/

If you want to add taxonomy filter as frontend filter using custom search, you need to create a custom search view and add taxonomy frontend filter:
=> https://toolset.com/documentation/user-guides/front-page-filters/
=> https://toolset.com/documentation/getting-started-with-toolset/filter-content-lists-and-add-custom-search/

#1227293

Hi Minesh, many thanks for the prompt reply. Sorry I have now found the option which was hidden under 'Screen Options'.
I have now set this up and it now filters all the post under this taxonomy.

How do you then add a dropdown category filter to this to further filter the results?

Many thanks in advance.

#1227305

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - you should add the frontend filters to your form. Please check the following Doc:
=> https://toolset.com/documentation/user-guides/front-page-filters/#vfmh-adding-search-controls

#1227315

Hi, I thought that but the Taxonomy is greyed out and I am not able to select it.

#1227338

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should remove the existing filter if you have added to "Search and pagination" section and also remove the "Query filter" for that taxonomy from "Query filter" section and save your view.

Then you should try to click on button "New filter" button to add your taxonomy filter (it will add related "Query filter" automatically).

#1227344

Hi Minesh, I have tried this but this then displays all of the CPT posts. I am trying to display all the post from the taxonomy 'Accommodation Type' and then filter by the accommodation type category.

#1227864

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - this is still unclear to me.

Can you please share few screenshots what exactly you want to display as output and problem URL where you added your view and access details that will help me to guide you in the right direction.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1227954

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I've added the following code to Toolset's "Custom Code" section:
=> hidden link

add_filter( 'wpv_filter_query', 'func_prefilter_post_taxonomy', 99, 3 );
function func_prefilter_post_taxonomy( $query_args, $view_settings, $view_id ) {

   $display_view_ids = array(2428);
     
    if (in_array($view_id,$display_view_ids) and empty($query_args['tax_query']) ){
      
      
      	 $query_args['tax_query'] = array(array(
                    'taxonomy'=> 'accommodation-type',
                    'field' => 'id',
                    'terms' => array(75,35,42,73,72,54,29,43,71,45,33,30,67),
                    'operator' => 'IN',
                    'include_children' => 1
                ));
		
    }
    return $query_args;
}

More info:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

Actually, you wanted to prefilter your view results based on your terms and again wanted to filter it using the dropdown select.

I've also changed the query filter section:
=> hidden link

Please check and let me know if its working as expected 🙂

#1228326

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check and confirm that solution I shared help you to resolve your issue 🙂

#1228408

Hi Minesh, yes that is perfect and works how I had planned. I have two more of these to create which now seems straight forward.
Where can I find the ID's for the post and taxonomy?
add_filter( 'wpv_filter_query', 'func_prefilter_post_taxonomy', 99, 3 );
I can find the view ID and the terms ID OK.

#1228443

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry? Do you mean that you want to locate the term ID? If yes - you can find it when you edit your term in address bar.

#1228459

Hi, I am trying to find the ID for the CPT and the Taxonomy.
func_prefilter_post_taxonomy', 99, 3 );

#1228463

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - the line of code you shared:

func_prefilter_post_taxonomy', 99, 3 );

99 and 3 is a priority of filter not a post or taxonomy Id. You do not need to modify it.

With the code I shared here:
=> https://toolset.com/forums/topic/display-all-posts-from-1-taxonomy/#post-1227954
You will see the following line where 2428 is the view ID. You can find the view ID when you Edit your view.

 $display_view_ids = array(2428);

You will also see the following line:

'terms' => array(75,35,42,73,72,54,29,43,71,45,33,30,67),

those are term IDs which you will find when you edit your term in the URL.

#1228480

Ok I see, I think I am nearly there.
I have created a 'Where to Eat & Drink' page but this also displays all the posts and not the posts from the 'food & Drink' taxonomy.
hidden link
Where am I going wrong. Thank you for you patience.