Skip Navigation

[Resolved] Mutliple Views for multiple CPTs within a "Search" filter

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

Problem:
How to create a parametric search that shows multiple custom post types which can be filtered by taxonomy

Solution:
Create a parametric search View.
For Content indicate that this view will display any of the required custom post types.
In the parametric search filters add one for each taxonomy you want to be able to filter by.
Add this View to a page where it can then be seen.

Relevant Documentation:
https://toolset.com/documentation/user-guides/front-page-filters/

This support ticket is created 7 years, 10 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 16 replies, has 2 voices.

Last updated by heatherS 7 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#397398

I am trying to: create a search filter that will display the 3 CPT's (types of content being White Papers, Videos, and Articles) based on the taxonomies Gender and Topic. We want the page (called Resource Center, here: hidden link) to display, by default, all posts from the 3 CPT's, the same way they are already displaying.

We want to add the search filter to the top of this page that, when a Gender and a Topic is selected, the posts from all three CPT's (White Papers, Videos, and Articles) that are marked as the selected taxonomies stay on the page, but the posts that don't fall under those taxonomies should disappear from the page.

I tried to create a search filter view that displayed the 3 views for White Papers, Videos, and Articles with an additional filter for each one that filtered by taxonomy that was dependent on the parent view, but this pulled up "no results found", so I removed this filter.

I visited this URL: hidden link

I expected to see: a search bar with all CPTs in each view showing, that filtered CPTs when taxonomies in the search filter bar were toggled.

Instead, I got: "No posts found" regardless of search filter selections

#397587

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

It sounds like you are most of the way there. I'll run through the steps to see if you recognise tripping up anywhere along the way:

- Create your CPTs for White Papers, Articles, and Videos
- Create your custom taxonomies for Gender and Topic and associate them with your CPTs
- Create some content for each of your custom post types and assign some taxonomy terms so that we have some content to filter.
- Now, create your View, which will be a parametric search View.
- Choose your 3 CPTs in the Content Selection section so that this view will return any of White Papers, Articles and/or Videos.
- For the search settings I think you want "AJAX results update when visitors change any filter values"
- Now add two filters, one for each custom taxonomy. Place your cursor between the wpv-filter-controls shortcodes and hit New Filter.
- Select your first taxonomy, eg. Gender.
- I added "Any" for the setting "Label for 'not selected' option:"
- Insert this filter and then repeat, adding another filter for your second taxonomy Topics.

At this point my filter section looks like this:

[wpv-filter-start hide="false"]
[wpv-filter-controls]
[wpml-string context="wpv-views"]Gender:[/wpml-string] [wpv-control taxonomy="gender" url_param="wpv-gender" type="select" default_label="Any" taxonomy_order="ASC" taxonomy_orderby="name" hide_empty="false"]
[wpml-string context="wpv-views"]Topics:[/wpml-string] [wpv-control taxonomy="topic" url_param="wpv-topic" type="select" default_label="Any" taxonomy_order="ASC" taxonomy_orderby="name" hide_empty="false"]
[/wpv-filter-controls]
[wpv-filter-end]

- Then design how you want your output to be displayed, either by adding shortcodes directly in between the wpv-loop shortcodes, or by linking to a Content Template (an existing one or choose to create a new one).

- You will then need to display your view somewhere. I created a dedicated page and used the Fields and Views shortcode button to add my view.

When I viewed the page the search worked exactly as you describe. If you follow these steps hopefully yours will too, but let me know if it doesn't.

#397690

Thank you for the step by step instructions!

I'm playing with this, but the 3 post types keep repeating on this page over and over making it hard to test. How do I prevent the 3 views from repeating on down the page?

#397874

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Heather

How are you inserting your View? If the content is repeating down the page it sounds like you have added a View within another (or the same) View, but I can't see.

I suggest you create a new page for testing and create a new View. You already have your CPTs, your custom taxonomy, and some existing content, so start at bullet 4 in the list above ("Now, create your View, which will be a parametric search View.") and follow the steps carefully.

I wrote those steps while going through the process myself, so I'm confident if you follow them you will get the desired result.

The penultimate step was slightly vague ("Then design how you want your output to be displayed"), so I will be a little more specific.

Let's explicitly create a Content Template to display summary information from our custom posts when listed in search results, via the Toolset > Content Templates screens. I'll call it "Search results item summary".

As you create it, "Don't assign it to any post type". Design the template. My ultra-basic template displaying a linked title and post excerpt looks like this:

<h4>[wpv-post-link]</h4>
[wpv-post-excerpt]

Save this, return to your View, place your cursor in between the [wpv-loop] shortcodes and add your Content Template using the button. For every result of our search ("iterating over the Loop") we should now see a linked post title and excerpt.

If that doesn't give you the result you are looking for then let me know.

#398070

Ah I think I know where I made a mistake here.

I inserted those 3 Views shortcodes into the Loop Output for the parametric search, so that must be what's the view inside of a view is.

The reason I did this was because we want the search results to look the same way they do here: hidden link

Each different Resource CPT (White Papers, Videos, Articles) is styled different and has different fields displaying. For example, the Videos need to show the body content, which holds the video embed code. Only White Papers need to show the featured image, etc. We also have 2 columns wrapped around White Paper and 3 around Videos.

The loop output of the parametric search results seem to affect the display of all of the types of Resources. It doesn't look like I can wrap a div around only the White Papers portion to style that differently, or display different fields for these.

Would I go about creating a Content Template for each type of Resource, then add their shortcodes into the search loop output?

#398075

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I can't see any of your links, I'm unauthorised, but I get what you are hoping to achieve.

So, your parametric search View is going to return a mix of White Papers, Videos, and Articles, and you want to display different content for each.

So, you make 3 Content Templates, one for the White Paper search results, one for Video search results, and one for Articles.

Now, in your View between the [wpv-loop] shortcodes where you assign the Content Template, use conditional shortcodes that effectively say if this iteration is a post type video then use the video Content Template, etc. etc.

See here for documentation of the conditional shortcode:
https://toolset.com/documentation/views-shortcodes/#wpv-conditional

#398107

Ok I've created those 3 content templates and have applied conditional logic to each type based on the taxonomy. My loop output looks like this

<wpv-loop>
        [wpv-conditional if="( '[wpv-taxonomy-slug]' eq 'white-paper' )"][wpv-post-body view_template="White Paper Content Template"][/wpv-conditional]
          [wpv-conditional if="( '[wpv-taxonomy-slug]' eq 'videos' )"][wpv-post-body view_template="Videos Content Template"][/wpv-conditional]
          [wpv-conditional if="( '[wpv-taxonomy-slug]' eq 'articles' )"][wpv-post-body view_template="Articles Content Template"][/wpv-conditional]
		</wpv-loop>

When I view it on the front page, nothing shows up in terms of posts. Only the search bar and filters appear. Toggling those does not affect the results beneath it.

Could the template usage for the Content Templates be off? They are currently set to Taxonomy archive: Resource Type

#398573

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You are querying posts, not taxonomies, and in your conditional shortcode you should be testing for the post type (using [wpv-post-type]) rather than the taxonomy slug.

Your parametric search query is going to return a mix of your 3 CPT *posts*, and you want to switch templates depending upon whether it is a white paper, video etc. Your search parameters are based on taxonomy but your search results are posts.

When you look at the content templates via Toolset > Content Templates they don't need anything checked on the Usage section (their use in Views isn't listed here), unless they are being used elsewhere, too.

#399334

Hmm the 3 Resource types are actually marked as taxonomies.

So we have one CPT - Resources.

Then we have 3 sets of taxonomies:
Types (White Papers, Videos, Articles)
Gender
Topics

Does this mean that I will need to have 3 separate CPTs for each type of Resource in order to get it working the way I described?

#399415

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Have you changed something?

Your original question begins "I am trying to: create a search filter that will display the 3 CPT's (types of content being White Papers, Videos, and Articles) based on the taxonomies Gender and Topic."

So I have been advising you on the basis of custom post types for White Papers, Videos, and Articles, organised with the taxonomies Gender and Topic.

If you can clarify how you have things set up and I can advise you on setting up your search view.

#399604

My apologies - I had the Resource types classified as taxonomies initially, but have created custom post types for these instead. I set up the filter and the conditional settings to display the appropriate view depending on Resource Type.

It looks like the last thing I need for this to work the way we are wanting is to get the posts to display in an order depending on their CPT instead of the post id, title, modification date, etc.

For example, we want all of the white papers to be first, then the videos, then the articles underneath. So three sections on the page.

I don't see a way to do this in the current "Order by:" settings.

#399764

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Not all of the WP_Query orderby parameters are currently available in the admin pages designing your views queries, and orderby post_type is missing, which is the one you need.

You can add a small snippet of code to your functions.php to modify your views query before it is passed to WP_Query using the filter wpv_filter_query (https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/)

You only want to alter the query for this specific view, so you will need the view_id (which you can see in your URL bar when editing the view).

/**
 * Custom function to orderby post_type
 */
add_filter( 'wpv_filter_query', 'order_by_post_type', 101, 3 );

function order_by_post_type( $query_args, $view_settings, $view_id ) {
	if ( 4 == $view_id ) { // change this for your view id
		$query_args['orderby'] = 'type';
	}
  return $query_args;
}

I've added a feature request to make this and other orderby parameters available when editing your View without the need for custom code.

#399908

Hi Nigel,

Thanks for providing the filter code needed.

I followed your instructions and added that snippet to the functions.php file of the theme, adding the View ID as well.

When I refresh the resource center page, the posts still order in the way they were previously.

Is there something in the View settings that I need to edit for this to work properly? I noticed that I can't deactivate the "Order by" field to not order by anything. I'm not sure if this could be overriding the ordering that the code snippet runs.

#399947

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Heather

Can you try again just changing the priority which I had as 99 and swap that to 101, hopefully that should do it.

I'm going to edit my previous answer and change the code accordingly for anyone else reading this thread.

#399972

Awesome, yup that worked!

I was able to add headers before each View when I had it configured that way, but it seems using the content template makes the header appear before each post as opposed to before each post section.

Is it possible to have a heading before each custom post type section? For example, "White Papers" before those CPTs, "Videos", etc.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.