Skip Navigation

[Resolved] Posts query

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

Problem:
The user wants to show related posts that have the same taxonomies terms and the same title

Solution:
Filtering the view with taxonomy filters can be done from the view's UI. But filtering with the same exact title needs a custom code. Check an example on https://toolset.com/forums/topic/posts-query/#post-1562743

This support ticket is created 4 years 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: Africa/Casablanca (GMT+00:00)

This topic contains 8 replies, has 2 voices.

Last updated by alfredoV-2 4 years ago.

Assisted by: Jamal.

Author
Posts
#1561071

Hi, i try to structure a toolset wordpress query that displays a specific taxonomy (A)+(B) of the post where it is executed and also the taxonomy (A)+(B) present in all the posts with the same title as the post being executed. How can I start setting up work? Thank you

#1561321

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Hello and thank you for contacting the Toolset support.

I am not really sure to understand your request, can you elaborate more? Are (A)+(B) your taxonomy terms? What do you mean by “the taxonomy (A)+(B) present in all the posts with the same title as the post being executed”

Thank you.

#1561439

OK
My Taxonomy terms is: AIRCRAFTS and NICKNAMES
The query display this terms (of the post) and all of the post with the same title of the current post.

#1561531

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Thank you for your feedback. But I'll still need more details to understand your request, especially, all of the post with the same title of the current post. The more details you provide the quicker we'll find a way to help you.

Can you please paste your Debug information in the Debug information field?
- https://toolset.com/faq/provide-debug-information-faster-support/

Can you also give an example maybe as follow:
- Post1: has terms xxx from AIRCRAFTS and terms yyy NICKNAMES.
- Post2: ...
- Etc.
I would like to have page xxx to display yyy and zzz.

Looking forward to your reply.

#1561865

OK
- Post1 (title A): has terms xxx from AIRCRAFTS and terms yyy from NICKNAMES of post1 and post2 (same title).
- Post2 (title A): has terms xxx from AIRCRAFTS and terms yyy from NICKNAMES of post2 and post1 (same title).

- Post3 (title B): has terms xxx from AIRCRAFTS and terms yyy from NICKNAMES of post3 (because there are no other posts with the same title).

- Post4 (title C): has terms xxx from AIRCRAFTS and terms yyy from NICKNAMES of post4 (because there are no other posts with the same title).

etc.

#1562381

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Thank you for your feedback. I'll write what I have understood so far and please correct me if I am wrong.

You want to display on a post single page, the posts that have the same taxonomies terms and the same title, right? So, with your example, we will have:
- Post1 page will also display Post2
- Post2 page will also display Post1
- Post3 page will not display any additional posts.
- Post4 page will not display any additional posts.
Is this what you are trying to achieve?

In addition, I would like to know how you show a single post page? Using a content template? Using a layout? Or maybe you are trying to add the additional post with a view?

Can you also give examples of the URLs that you will have for your pages, that could help me understand better the structure and views that you are building?

Can you also paste your Debug information in the Debug information field?
- https://toolset.com/faq/provide-debug-information-faster-support/

#1562385

I want to display on a post single page, the posts that have its own title, and view only the taxonomy X and Y of the posts.

I have an elementor post template and I need to integrate a view into it.

Thanks

#1562743

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

I was able to create a similar view on my local setup with 4 posts.
First I created a view that is filtered with taxonomy terms related to the current post, check this screenshot hidden link

Then I integrated this view inside my Custom Post Content template. At this stage, the view shows all the posts that have the same AIRCRAFTS and NICKNAMES terms.

Then I had to add the following code to add a custom search on post title and only include the posts that have the same exact title, please note that you will need to adapt the view ID of the code with your View ID, check this screenshot hidden link

function func_search_by_exact_title( $search, $wp_query ){
	global $wpdb;
	global $WP_Views;
	global $post;
	
	if($WP_Views->current_view == 1065){
		// if ( empty( $search ) )
		//	return $search; // skip processing - no search term in query
		
		$current_post = get_post( $post->ID );
		$term = isset( $current_post->post_title ) ? $current_post->post_title : '';
		error_log(print_r(array("term" => $term, "current_post" => $current_post), true));
		$search = " AND ($wpdb->posts.post_title REGEXP '[[:<:]]{$term}[[:>:]]')";
	}
	
	return $search;
}
add_filter( 'posts_search', 'func_search_by_exact_title', 1000, 2 );

This helped me show:
- Post 2 inside Post 1 as they have the same title "Title A", I have also added the Post ID to distinguish between them, check this screenshot hidden link
- Post 4 has no additional posts because no other post has the title "Title C", check this screenshot hidden link

I did not use Elementor for my test, to integrate this inside an Elementor template, you can use a shortcode as follow and elementor’s shortcode module:
https://toolset.com/documentation/user-guides/using-toolset-with-elementor-page-builder/build-custom-queries-to-list-posts-with-elementor-and-toolset/

[wpv-view name="Name or slug of my related posts view"]

Let me know if this helps.

#1564235

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.