Skip Navigation

[Résolu] Display posts based on parent's taxonomy

This support ticket is created Il y a 7 années et 6 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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+01:00)

Marqué : 

This topic contains 5 réponses, has 2 voix.

Last updated by Nigel Il y a 7 années et 6 mois.

Assisted by: Nigel.

Auteur
Publications
#443127

I am trying to: display posts that are filtered by the parent post's taxonomy terms

this is a follow-up to my earlier post:
https://toolset.com/forums/topic/list-child-posts-based-on-parents-taxonomy

For context: I have a post type 'Performance' with parent post 'Artist'. Artists have taxonomy 'genre'. On a performance post, I'd like to display related posts - based on the Artist's taxonomy terms.

the recommendation from Minesh was to create a custom function. I have hired a developer to do this, but he's getting stuck. here is what the developer has said:

----------
Main global post is of post type 'performance', presented on it's own template. Below it I want to show a sort of similar/related posts. The idea is to have one views loop for parent post type 'artist' and nested in that another loop for 'perfomances'. Only parent post type has a 'genre' taxonomy set and ideally the nested loop would only show performances that share genres of parent artist post.

It sort of works using custom function on conditional wrapped around the output of 'performance' post (the nested one) The function grabs both global posts and related posts parent and compares arrays of taxonomy 'genre' Here's the code for it:

function filter_tester() {

$postid = get_the_ID();
$parentid = get_post_meta ( $postid, '_wpcf_belongs_artist_id', true );
$parent_genre = wp_get_post_terms ( $parentid, 'genre', array ('fields' => 'names') );

wp_reset_postdata();


global $post;
$singleid = get_the_ID();
$single_parentid = get_post_meta ( $singleid, '_wpcf_belongs_artist_id', true );
$single_genre = wp_get_post_terms ( $single_parentid, 'genre', array ('fields' => 'names') );

wp_reset_postdata();


$compare = array_intersect( $parent_genre, $single_genre );
if ( !$compare ) {
return false;
} else {
return true;
}

}

Because it's ran inside the nested loop, it's hard to control the number of posts to retrieve, at least I wasn't able to figure it out so far. Also when custom function is filtering the results using wpv-conditional tag, all the thumbnails of posts are showing the first one in set.
----------

Do you have any thoughts on how I can get this working?
I can provide you with access to the site.

Thank you,
Jeff

#443362

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

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

Hi Jeff

I would just like to describe in words what I think you want so that you can confirm that before I suggest a solution.

You have a parent CPT of Artist and a child CPT of Performance, and the Artist has a custom taxonomy of Genre.

So you might have an Artist post of 'Kate Bush' with the genre terms of 'Female Artist' and 'Piano', and there is a child Performance post of 'Live at the O2'.

Among various other content you have an Artist post 'Nils Frahm' who also has the genre term 'Piano'.

Where you display a single Performance (let's say you have a Content Template for single Performance posts) you want to add a View of related performances, where related performances means show Performances of Artists who have genre terms which match the genre(s) of the Artist that is the parent of the currently viewed Performance.

So, if you were viewing the performance 'Live at the O2' your related performances view would list all performances of Nils Frahm plus those of other Artists with genre terms of Female Artist or Piano. (Or would it only show performances of Artists with both genre terms Female Artist and Piano?)

Does that sound right?

#443439

Hi Nigel,

Yes exactly!

I'd be fine if it were "and/or" if there were multiple genre terms. So as long as they have at least one matching term.

Thanks!
Jeff

#443808

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

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

OK Jeff,

I think some of this you can do yourself, there is really just one step that is problematic that will require a custom solution, and I will address that below.

So, you make a Content Template to display single Performances and add your desired content to it.

You then want to add a view for your related performances.

This will actually require two views, nested.

The outer view which you add to your content template will return Artists. For the time being don't worry about adding a query filter, we will display all Artists for the time being. In the loop output section we don't output any content directly, but instead insert the shortcode for our inner view.

That inner view will return Performances, and needs the filter to show posts which are children of the current item in the loop. The loop output section is where you will add shortcodes to output whatever summary info you want to appear about your related performances.

Test that this works. Right now related performances should show ALL performances, because our outer loop is getting all the artists, and then the inner loop is showing all the performances for a given artist.

So what is needed now is some custom code to limit the artists retrieved in the outer view to only those required, e.g. to artists with taxonomy terms that match the parent artist of the performance being shown by the content template.

To do that we will need to modify the query for the outer artist view (so make a note of the id of that view).

Ordinarily I would at this point say we can use the wpv_filter_query filter to modify the query arguments but discovered (and reported) a bug whereby the filter isn't available if the view is added to a content template, which is the case here.

So, change of tack.

Where you add the outer view for the artists, we will use a custom shortcode to retrieve the ids of the related artists which we pass to our view via a shortcode argument, and then we need to add a query filter on that view which filters by post id, based upon the shortcode.

So, the outer view now has this filter: Include only posts with IDs set by the View shortcode attribute "ids" eg. [wpv-view name="view-name" ids="1"]

And our content template that calls this view will do so with a custom shortcode argument like so:

[wpv-view name="related-artists" ids="[related_artists]"]

And then here is the code for that custom shortcode.

/**
 * Related Artists shortcode
 */
function related_artists_shortcode ( $atts ){

	global $post;

	$performance_id = $post->ID;
	$parent_artist = get_post_meta( $performance_id, '_wpcf_belongs_artist_id', true );

	$artist_genre = get_the_terms( $parent_artist, 'genre' );

	$artist_genre_ids = "";

	foreach ($artist_genre as $genre) {
		
		$artist_genre_ids .= $genre->term_id . ",";
	}

	$artist_args = array(
		'post_type'		=>	'artist',
		'post_status'	=>	'publish',
		'tax_query'		=>	array(
			array(
				'taxonomy'	=>	'genre',
				'field'		=>	'term_id',
				'terms'		=>	$artist_genre_ids
			)
		)
	);

	$related_artists = get_posts( $artist_args );
	$related_artist_ids = "";

	foreach ($related_artists as $related_artist) {
		
		$related_artist_ids .= $related_artist->ID . ",";
	}

	return $related_artist_ids;
}
add_shortcode( 'related_artists', 'related_artists_shortcode' );

I tested this and it worked for me. If you have problems with it hopefully you will be able to solve them yourself or with your developer as I already spent longer than I ought to have on the solution.

Good luck!

#444905

Hi Nigel,

This almost worked for me. I'm having a little difficulty tho.

When I get to the last part and try adding in the reference to the shortcode, it just spits out:
"]

It's like it's not registering the shortcode. I added the code to a plugin that adds custom code (like adding it to functions.php)

Any quick thoughts on why this might be happening?

Thanks,
Jeff

#445207

Nigel
Supporter

Languages: Anglais (English ) Espagnol (Español )

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

Hi Jeff

Couple of things.

I probably didn't mention that you need to register custom shortcodes that are to be used inside views shortcodes. Go to Toolset > Settings > Front End Content and add related_artists (no brackets or quotes).

If you are still having problems try modifying the shortcode code temporarily so that the last line which returns $related_artist_ids returns a string with an id instead, e.g. "76" and see what happens.

I don't know about the custom code plugin you are using, you may need to add the code to functions.php.

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