Skip Navigation

[Resolved] view for displaying related products based on having the same custom field value

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.

Our next available supporter will start replying to tickets in about 7.06 hours from now. 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 6 replies, has 3 voices.

Last updated by Minesh 4 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2707443

Hi there,

I want to display "related products" on the product page. the related products all share the same value of a custom field of the product that you are currently viewing.

I have tried the various filtering options, and cannot find any that works, and not sure the best way to do this please can you advise.

Regards,
Vince

#2707459

Nigel
Supporter

Languages: English (English ) Spanish (Español )

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

Hi Vince

You are displaying a product, and that product has some value for a custom field.

You want to show other products that also have the same value for that same custom field, yes?

As a Lifetime user I'm guessing you are familiar with the legacy Views editor and inserting Views via the wpv-view shortcode.

You need to create a View to display products, and include a Query Filter for the custom field in question.

But what value should it filter by?

Well, you need to pass the value to the View via shortcode attribute, so you will specify in the Query Filter a shortcode attribute that it should get the value from, and then you will provide the value by adding the attribute to the wpv-view shortcode where you insert the View.

(You can read more about that here: https://toolset.com/documentation/legacy-features/views-plugin/passing-arguments-to-views/)

The last point is what value to pass via the attribute. It should be the value of the field for the current product being displayed.

So you can generate that value via shortcode, probably a good idea to use the wpv-post-field shortcode (it generates the raw field value, which is what you would need to filter by), so that your View would be inserted via a shortcode something like:

[wpv-view name="Related Products" filterby="[wpv-post-field name='wpcf-my-custom-field"]

Note with the wpv-post-field shortcode you need to use the wpcf- prefix with your field slug.

#2707468

Hi Nigel,

This was one of the solutions I previously tried, and it didn't work, so didn't know if I was missing something.

for example I have:

[wpv-view name="product-related-v1" filterby="[wpv-post-field name='wpcf-first_four_chars"]

but it still shows all the results and not the filtered results.

I even tried to insert it as a shortcode attribute using the newer block system but it says the shortcode attribute can only be letter.

#2707485

Nigel
Supporter

Languages: English (English ) Spanish (Español )

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

Can you share a screenshot of the Query Filter settings you used?

#2707486

Nigel
Supporter

Languages: English (English ) Spanish (Español )

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

Also, did you copy and paste the wpv-view shortcode into your reply or type it?

It's wrong, missing the closing ' and ] in the wpv-post-field shortcode.

It would be

[wpv-view name="product-related-v1" filterby="[wpv-post-field name='wpcf-first_four_chars']"]
#2707974
screen3.png
screen2.png
screen1.png

Hi Nigel,

I did already try that with and without the closing ' and ] incase that was something related to it not working. here is the screenshots. I have tried it both as a shortcode on the page, as well as using a content template where I contain the view query.

I have left the query filter at the top of the view blank, because I couldn't see that there was a way for me to query it based on that field entry value.

Any ideas why is it not working from your filter instruction?

#2708074

Minesh
Supporter

Languages: English (English )

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

As I can see with the screenshot you shared:
- https://toolset.com/wp-content/uploads/2024/07/2707974-screen2.png

You do not added the "Query Filter" for your custom field within the "Query Filter" section.

Can you please follow the following Doc and try to add the "Query Filter" for your custom field and filter by shortcode attribute name "filterby":
- https://toolset.com/documentation/legacy-features/views-plugin/passing-arguments-to-views/#filters-by-custom-fields

#2708316

Great thanks I thought I was missing something somewhere, you may want to update the details on that link you provided, as it doesn't explain to include the shortcode in the query filterby.