Skip Navigation

[Resolved] Filter Taxonomy view by Custom Post Type

This support ticket is created 2 years, 11 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 12 replies, has 2 voices.

Last updated by umbertoZ 2 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#2069807

Hi, I've a Places CPT and various Custom Taxonomies like: Province, Type, Services.

I've 1000 Places, each one of them connected to a Province Term.

In Province archives I want to display all the province's Places and I also want display stats about Types and Services.

For example:

Madrid Provinces
100 Places
Types: 30 Museums, 40 Restaurants, 20 Discos
Services: 20 Taxis, 30 Rutes, 50 Rent bikes

I built the Archive loop for places, then I built 2 Taxnomy views for Types and Services where I display the terms with counters.

My problem is that the counters always display the total number of Places connected to each term, not only the number of Places of the current Province. Is there a way to filter it?

thanks

#2070081

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To suggest the best way to achieve this, I'll need to see exactly how these views are set up on the taxonomy archive pages.

Can you please share temporary admin login details along with the link to an example archive page for a province?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#2071167

Hey, is there a solution?

cheers

#2072369

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these details and sorry about the delay.

I'm currently performing some tests with similar views on my website and will share my findings with you shortly.

Thank you for your patience.

#2073293

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting and here are my findings.

On the homepage, you need to show the total number of taxonomy term items, which is why using the taxonomy views work.

However, on the province archive pages, you need to show the count of those 'Vertederos' posts, which have a particular province, service, and type terms, which means that you'll need a post view with taxonomy filters and not the taxonomy views.

As the home page and these archive pages have a different requirement, you can create a duplicate of your content template "Home - Números" ( /wp-admin/post.php?post=1534&action=edit ). The original will continue to be used for the homepage.

The new duplicate will be used for the archive pages and in that you'll call the post view instead of the taxonomy view.

In your content template "Cabecera - Archive" ( /wp-admin/admin.php?page=ct-editor&ct_id=2301 ), you're already passing the current province's term slug in the shortcode attribute 'wpvprovincias' for the view 'vertederos-estadisticas'.

In your duplicated content template, when you'll be calling the post view, you can pass the current province's slug value again, using the 'wpv-attribute' shortcode, like this:


[wpv-view name="post-view-name" cached="off" wpvprovincias="[wpv-attribute name='wpvprovincias']"]

As a result, in your new post view, you'll have the province slug and you can pass on the service and type terms slugs in the view's shortcode too, to get the desired 'Vertederos' posts.

I hope this makes sense and please let me know if you need any further assistance around this.

#2073371

Hi, thanks for your reply. I'm not sure I'm understanding.

Should I add a different post view for each stat number, where I filter by taxonomy and display the [wpv-found-count]?

I was trying to avoid this solution to reduce the number of view calls to the server. My plan was to use just 2 taxonomy views and the [wpv-taxonomy-post-count]. If I use post views I should add 6 views.

Am I understanding well the solution?

Am I wrong about the site performance? Does it makes sense to reduce the views on the page?

cheers

#2073463

Hi, thanks for your reply. I'm not sure I'm understanding.

Should I add a different post view for each stat number, where I filter by taxonomy and display the [wpv-found-count]?

I was trying to avoid this solution to reduce the number of view calls to the server. My plan was to use just 2 taxonomy views and the [wpv-taxonomy-post-count]. If I use post views I should add 6 views.

Am I understanding well the solution?

Am I wrong about the site performance? Does it makes sense to reduce the views on the page?

cheers

#2073477

Hi, thanks for your reply. I'm not sure I'm understanding.

Should I add a different post view for each stat number, where I filter by taxonomy and display the [wpv-found-count]?

I was trying to avoid this solution to reduce the number of view calls to the server. My plan was to use just 2 taxonomy views and the [wpv-taxonomy-post-count]. If I use post views I should add 6 views.

Am I understanding well the solution?

Am I wrong about the site performance? Does it makes sense to reduce the views on the page?

cheers

#2073581

Hey, I'm sorry I sent 3 times the same message, but I cannot see it after submit. It seems that the support topic is cached, so it doesn't display the most recently sent message.

#2074659

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

We're aware of a cache issue that sometimes causes the new messages to show up after some delay and it is being worked on.

If you don't want to use multiple post views as I suggested in my last reply, an alternate can be to use a custom shortcode that can give you the count of the location posts, with respective taxonomies.

Custom shortcode:


add_shortcode('get_post_count_by_tax', 'get_post_count_by_tax_func');
function get_post_count_by_tax_func($atts) {

	$a = shortcode_atts( array(
		'province' => '',
		'type' => '',
		'service' => '',
		'post' => '',
	), $atts );

	// change these taxonomy slugs
	$province_slug = 'add-province-tax-slug';
	$type_slug = 'add-type-tax-slug';
	$service_slug = 'add-service-tax-slug';

	$tax_query = array('relation' => 'AND');

	if(!empty($a['province'])) {
		$tax_query[] = array(
			'taxonomy' => $province_slug,
			'field'    => 'slug',
			'terms'    => $a['province'],
		);
	}
	if(!empty($a['type'])) {
		$tax_query[] = array(
			'taxonomy' => $type_slug,
			'field'    => 'slug',
			'terms'    => $a['type'],
		);
	}
	if(!empty($a['service'])) {
		$tax_query[] = array(
			'taxonomy' => $service_slug,
			'field'    => 'slug',
			'terms'    => $a['service'],
		);
	}

	$args = array(
		'post_type'        => $a['post'],
		'posts_per_page'   => -1,
		'post_status'      => 'publish',
		'fields' 		   => 'ids',
		'tax_query'        => $tax_query
	);

	$posts_array = get_posts( $args );

	return count($posts_array);
}

Please replace "add-province-tax-slug", "add-type-tax-slug", and "add-service-tax-slug" with the actual slugs of your website's province, type, and service taxonomies, respectively.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

This custom shortcode accepts 4 parameters:
- post: slug of the post type to get the count from
- province: slug of the target term from the province taxonomy
- type: slug of the target term from the type taxonomy
- service: slug of the target term from the service taxonomy

Here are some usage examples:

To get the count of all posts in the "listing" post type with the province taxonomy term slug 'abc':


[get_post_count_by_tax post="listing" province="abc"]

To get the count of all posts in the "listing" post type with the province taxonomy term slug 'abc' and type taxonomy term slug 'def':


[get_post_count_by_tax post="listing" province="abc" type="def" ]

To get the count of all posts in the "listing" post type with the province taxonomy term slug 'abc' and service taxonomy term slug 'ghi':


[get_post_count_by_tax post="listing" province="abc" service="ghi" ]

To get the count of all posts in the "listing" post type with the province taxonomy term slug 'abc', type taxonomy term slug 'def', and service taxonomy term slug 'ghi':


[get_post_count_by_tax post="listing" province="abc" type="def" service="ghi"]

I hope this helps and please let me know if any point is not clear.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2075211

Thank you!! The shortcode works fine. I had to add it 6 times to get all the stats I need to display.

Is it more efficent then 6 Views? or it makes the same number of calls?

cheers

#2075501

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for the update and glad that it worked.

Since the custom shortcode uses WordPress functions directly, it should be more efficient than using separate views.

Views are designed to fulfill a larger purpose of creating a dynamic list of content with many customization options. Calling them repeatedly, only to get the count of posts, sounds like an overkill.

#2075557

My issue is resolved now. Thank you!

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