Skip Navigation

[Resolved] Related blogposts on product detailpage

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by dimitriH 1 year, 3 months ago.

Assisted by: Minesh.

Author
Posts
#2525655

Tell us what you are trying to do?

I'm trying to show related blogposts (based on tag) on a product detailpage where product name equals tag name.
I've set up blogposts with a custom tag that is the same as the product name.

Example:
On hidden link (product detail page), I want to show blogposts with tag 'acrylinject-r'.

How do you set up the view's query filter to achieve this?

What is the link to your site?
hidden link

#2526257

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

On your product details template you should try to add a view and set it to query the content type (post type) you want to display the posts and then add a taxonomy filter for tags and select the option "Value set by the current page where this View is shown".

More info:
- https://toolset.com/documentation/legacy-features/views-plugin/filtering-views-by-taxonomy/

Please check how you can create view:
- https://toolset.com/course-lesson/creating-a-view/

#2526273

Hi Minesh,
Thanks for looking into this.

Your proposed solution requires both content types (blogposts and products) to use the same tag.
Is it possible to use the tag's slug (blogposts) and get it from the URL (products) for comparisson?

To clarify:
Blogpost has tag 'Acrylic R'. So, the slug for that tag is 'acrylic-r'.
The product URL is '/product/acrylic-r/'
Is it possible to get the second part from the URL as well?
I feel - since this is a permalink - we should be able to fetch a parameter from the URL.
Maybe something like wpv-tag-slug?

Cheers

#2526299

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What posts you want to display, you want to display posts from default post type post or from the product post type?

As I understand - you want to display posts from default post type post that has the tag assigned as "acrylic-r"and you have a product with title that has "acrylic-r" - correct?

#2526327

Correct Minesh.
I want to display default post type and we've set up 'product' as a custom type (not WooCommerce's post type 'product').

Default post type:
Tag ="Acrylic R"
Tag slug ="acrylic-r"

Custom post type (product):
URL: /product/acrylic-r/

#2527085

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You can use the view's filter "wpv_filter_query" hook to filter the view query on fly.

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

You should create a view by adding the view block to your single product details content template and set the view to query the default post type "post" add the things you want to display within the loop editor.

Then,
You can add the following view filter to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

add_filter('wpv_filter_query', 'func_show_related_posts', 99, 3);
function func_show_related_posts( $query_args, $view_settings, $view_id ) {
    
    if($view_id == 99999){

      $tag_slug = explode("/",$_SERVER['REQUEST_URI']);

$found_tag = get_term_by('slug', $tag_slug,'post_tag');
if($found_tag) {
      
      $query_args['tax_query'] => array(
        	array(
            	'taxonomy' => 'post_tag',
            	'field' => 'slug',
            	'terms' => $tag_slug,
           		'operator'=> 'IN'
        	));
     }
   }
    return $query_args;
}

Where:
- Replace 99999 with your original view ID

Check on frontend I hope the solution shared above will help you to resolve your issue.

#2527161

There are 2 small errors in the snippet you provided.
A syntax error on line 11 ("=>" should be "=")
Your passing an array to get_term_by() while it is expecting a string.

This snippet is working for us:

add_filter('wpv_filter_query', 'func_show_related_posts', 99, 3);
function func_show_related_posts( $query_args, $view_settings, $view_id ) {
     
    if($view_id == 713){
 
      $tag_slug = explode("/",$_SERVER['REQUEST_URI']);
	  //print_r($tag_slug); 
	  //RESULT: Array ( [0] => [1] => product [2] => acrylinject-r [3] => )
	
      $found_tag = get_term_by('slug', $tag_slug[2],'post_tag');

	  if($found_tag) {
       
      $query_args['tax_query'] = array(
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' => $tag_slug,
                'operator'=> 'IN'
            ));
     }
   }
    return $query_args;
}

Thanks for pointing us in the right direction, Minesh.

#2527163

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.