Skip Navigation

[Resuelto] Query Filter to display only related child posts?

This support ticket is created hace 7 años. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Etiquetado: 

Este tema contiene 12 respuestas, tiene 2 mensajes.

Última actualización por Charles hace 6 años, 11 meses.

Asistido por: Nigel.

Autor
Mensajes
#594542

Support,

Is there an option to add a Query Filter to display only related child posts?

My goal is to display 4-6 related posts at the end of the active/current post with the same child category.

Thanks,
Chuck

#594678

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Hi Chuck

Can you clarify exactly what you are aiming for?

Where you say "related child posts" that sounds like you have set up parent-child post relationships and when viewing a child post you want to display siblings (i.e. other children of the same parent post).

But then you say "with the same child category", which sounds like something else.

Can you give a concrete example?

#594895

Nigel,

Apologies for the mixed language.

Here is the URL of an example post: enlace oculto

At the bottom of that post I am trying to display 4 Ashtray posts, 2x2 with a "more" button leading to ashtray archives. The site consists of a few parent categories and each parent has multiple children. I'm trying to create one view/template that will display posts related to the child category.

Thanks,
Chuck

#595097

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Screen Shot 2017-12-01 at 13.49.47.png

Hi Chuck

This may not something you can readily do in Toolset without writing custom code. The problem is that when getting the categories of a post there is no distinction made about the position of the category in any hierarchy (https://developer.wordpress.org/reference/functions/get_the_category/).

I assume that for this example post it has both the cigar and the ashtray categories, yes?
enlace oculto

You can create a View that you display on the same post which has a Query Filter to display posts with categories the same as the post where it is displayed (see screenshot).

But the current post with that example has the categories "cigar" and "ashtray". So the View will find posts that have categories "cigar" or "ashtray".

I assume you want it to only show other posts that have the "ashtray" category.

If you want that then you will need to code a custom solution.

If your related posts can be any other cigar-related post then the above will work for you.

#595145

Got it. Sounds like I need a custom solution. Do you have any insight as where to begin?

Thanks,
Chuck

#595678

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Hi Chuck

Sorry, I didn't manage to get to this today, I'll try and propose something for you tomorrow.

#595679

Nigel,

No worries. Your help is greatly appreciated!

Thanks,
Chuck

#596365

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Hi Chuck

I came up with the following code example for you.

You will need to edit the View ID.

Please note that providing custom code is outside of our support policy and this is intended to help you get started if the code needs modifying in any way.

The intended use is that you insert a View for related posts which has a Query Filter to display posts within the same category as the current post.

It then removes the top level categories from consideration.

So if you have an ashtray post which is categorised "cigar" and "ashtray" and you add a View as described above, this code will remove the parent "cigar" category from the matches, so it will find other posts with the ashtray category, but not lighter posts which also have the cigar category.

function skip_top_level_categories( $query_args, $view_settings, $view_id ){

	if ( 39 == $view_id ){

		global $post;
		$child_terms = array();

		$terms = wp_get_post_terms( $post->ID, "category" );

		foreach ($terms as $key => $term) {
			
			if ( 0 != $term->parent ) {

				$child_terms[] = $term->term_id;
			}
		}
		$query_args['tax_query'][0]['terms'] = $child_terms;

	}
	return $query_args;
}
add_filter( 'wpv_filer_query', 'skip_top_level_categories', 101, 3);
#596516

Nigel,

Thanks! I'll give this a try this evening.

Any updates on this thread: https://toolset.com/forums/topic/wrap-flexslider-in-custom-url/
I see it's marked closed. Not sure if I closed it by mistake or support did.

Thanks,
Chuck

#597236

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Hi Chuck

Let me know how you get on here and if you need more help or we can close this, thanks.

#597557

Nigel,

Per your instructions, I change this line:

    if ( 39 == $view_id ){

I changed 39 to 9094 (my view ID), but no changes occurred. Is there additional/different lines that need to be changed?

If this is out of scope please close the ticket.

Thanks,
Chuck

#597740

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Hi Chuck

I looked at the code I suggested... and there is a tiny but crucial typo.

In the last line the name of the filter is "wpv_filter_query", it's missing a T.

#597756

That did the trick! Thank you for your help.

Best,
Chuck