Skip Navigation

[Resolved] How to display related Posts based on custom fields?

This support ticket is created 5 years, 1 month 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 24 replies, has 2 voices.

Last updated by Beda 5 years, 1 month ago.

Assisted by: Beda.

Author
Posts
#1347901

Please see here what I am doing currently to resolve this issue, which probably cannot be resolved like this.

I will also try to find any other possible approach to list posts by similarity of their RFG's, and share it here, if I find a solution.

Thank you

#1347995

So, here is what I found.

1. You can search one custom field by many values. This is possible in Toolset natively with normal custom fields or by using for example a Query Filter by URL parameter and crafting a URL like this:

page-with-view/?your-url-search-argument-for-field[]=the_value_you_search&your-url-search-argument-for-field=some_other_value_you_search

2. Above will return all posts with fields with those values. So, we can use that also on RFG but, you cannot produce the FRONT END filter with the GUI, that would need to be done manually.
To simply return those posts however, you can follow below steps. Note I outline all steps from 0 to achieved, so in case you want to adapt your current setup you have a reference.

A) Create a Post Type and a RFG attached to that Post Type.
B) The RFG has a single line, for example
C) Create many posts with many RFG and each several Single Lines with values (some the same as on other posts).
D) Create a View, and query the RFG post type
E) In the View set a query filter to return only those posts with the Custom Field (the single line in the RFG) equal to the URL parameter you set
F) Complete the Loop of this View with information either about RFG, the field, or parent post data
G) Insert this View to a Page, and then visit the page. Now you will see all the RFG's - not only the ones with the same value.

You can now add ?your-url-search-argument-for-field[]=the_value_you_search&your-url-search-argument-for-field=some_other_value_you_search (and repeat ?your-url-search-argument-for-field[]=the_value_you_search as many times you want) to the URL, to find all those posts, with the values in the fields as set in the query arguments.

You cannot do that in a ShortCode attribute, so when inserting such a View to a Post, to show only the Posts that have the same fields values as in that current post, you would need to call those single posts in the browser with the correct URL parameter.
There are different ways to do that, important is that the single posts, always would be linked to with the URL parameters appended.

Note that you would need another View, querying the current posts's RFGs to output it's fields and put them into the URL parameters.

All in all, it is not an easy solution, and it is pushing the intentions of Repeatable Field Groups to the limits, but it is possible to achieve.

A better way to display a list of "similar" posts would be to use Taxonomies.
That is used in WordPress generally to express "similarity" between items (so called categories, or taxonomies).

Another way is to use relevannsi to enhance Toolset's Search (because Toolset, after all, cannot search by "similar", but only by "is, greater than, lower Than etc"). With Relevannsi you can search fuzzy and with weight, and more.
But this features of Relevannsi only will work on textual fields searched with a Text Search input in Toolset, and hence of no real help here.

The best way to express similarity and list such simile posts is to use taxonomies, instead of Repeating Field Group values.

#1348653

Hi, Thanks for your reply.
See my Job type field is a 'select field type' i.e. a list of text values.
Now what you have suggested as your first option seems to be critical for me. Actually I don't know where to add the code

?your-url-search-argument-for-field[]=the_value_you_search&your-url-search-argument-for-field=some_other_value_you_search

It would be convenient for me if you add the same code in the 'related post' view or where it fits so that it serves the purpose.

#1348781

That is not code.
It is an URL Argument. It gets added to the URL, this is the idea.

If you have a RFG, and create a View for those RFG, then you can add **Query Filters** to search one or more of the custom fields in the RFG, by one or many values.
This query has to listen to an URL Argument.
Since you need to pass dynamic value to the Query (the value of the fields of the RFG of the current post), you need an URL argument for that to tell the View, get only those RFG where their fields have value equal to the one passed in the URL argument,

Hence, you create such a Query filter in the view by URL argument for the field as I explained above.
The, you need to (when viewing this View in the front end) tell the view what to filter by, by adding to the URL, the URL parameters you have set in the View, example:
page-with-your-view/?your-url-search-argument-for-field[]=the_value_you_search&your-url-search-argument-for-field=some_other_value_you_search

If you now hit enter and load that URL, it returns all posts with fields with those values.

You cannot produce this filter in the FRONT END for the visitor to alter, the Developers are working on this.

The steps below are leading to the result:

A) Create a Post Type and a RFG attached to that Post Type.
B) The RFG has a single line, for example
C) Create many posts with many RFG and each several Single Lines with values (some the same as on other posts).
D) Create a View, and query the RFG post type
E) In the View set a query filter to return only those posts with the Custom Field (the single line in the RFG) equal to the URL parameter you set
F) Complete the Loop of this View with information either about RFG, the field, or parent post data
G) Insert this View to a Page, and then visit the page. Now you will see all the RFG's - not only the ones with the same value.

Note that you would need another View, querying the current posts's RFGs to output it's fields and put them into the URL parameters.
This means, when you create the Links to the Posts passing the URL arguments, you need a view that gets that posts RFG' fields, and puts them in the URL syntax as I explained above.

As I mentioned this is not a simple solution and requires at least one more view to be created, to create the URL parameters for each post. Then you need to update wherever you link to those single posts (that may be the archive) so the links there pass the URL query argument required for the views to query the RFG's fields value.

Where do you link to those posts from?
That is where you would need to start, by changing those links and making them be like:
site.com/page-with-your-view/?your-url-search-argument-for-field[]=the_value_you_search&your-url-search-argument-for-field=some_other_value_you_search

Currently likely you use something like [wpv-post-link] in an archive, so you will need to change that and use Manual HTML links that you can populate with partially dynamic shortcodes and the above URL arguments.

Once that is done, you will be able to insert the View querying RFG?s to the single posts templates, and when those are visited with the right URL parameters, the right related Posts will show.
If the post is called only with the permalink, no URL parameters added, then all the RFG posts would show, not only related ones.

This could be finally solved with a HTML condition, that listens to the URL parameters as well, and if empty, it would hide the entire View (so no posts are shown as related unless the Query Arguments are present)

I suggest we tackle this last.

#1350769

Hi,
I tried but couldn't make the way you want the URL argument to be inserted. I would appreciate if you can do it on my behalf. Thanks.

#1350775

I can do so, could you link me to the View where you added the Filter, and want to query and display the RFG data, and to the post where this is visible - additionally to the place where from you link to that post.

A single post usually cannot be found in your site unless you link to it from an archive for example.

The workflow needs to be like this:

1. The visitor visits your site and see a list of links to single posts
2. That list allows us to edit the HTML and put the URL parameters in place
3. Then the View can filter the results, otherwise it will not be able to do so

Note that the Developers just fixed 3 issues, and will release this soon.
It will afterwards be possible to create Views for RFGs, that you an query by their "parent" post (the post they belong to), both in the backend and front end!
As well, you will be able to filter in the front end by the Fields in those RFGs.
Additionally the issue will be fixed where you would not be able to display parent post data in the RFG View using then GUI.

Maybe, if you can wait until 2.9.3 Views is released (see https://toolset.com/errata/views-has-issues-when-creating-repeatable-fields-groups-fields-filters-and-when-displaying-the-view/), you could use the new features to create a Search instead, which would avoid the need of editing the HTML links (You could then create a front end search, by the fields in the RFGs, which allow either the visitor to find the results by searching, or we can set default values, for example, and hide the search inputs.

I cannot state an ETA for that release, but it should not take too long.

In any case if you want to proceed with the current setup we can do so, I will just need the place where we will insert the links, the place it should lead to (this is any of those posts in that type, of course) and the View you used to add the query filter.

Thank you!

#1350821

So nice of you. You relieved me with your information. If the problem would be sorted out with the next plugin update, I don't think of making unnecessary modification necessary at all. Let's wait.
But I want to request you to keep the ticket open till we resolve the issue. Hope that wouldn't make you bother. Till then can I close your admin access to my site. Thanks.

#1350825

I can unfortunately not interfere with the closing/clean up robot here, he will try in about 3 days to close the ticket for the first time.

What you can do is open a new one in case with the updates you would not be able to proceed despite the changes, or would need more help.
You can of course also reply here as soon the robot sent the email to close this ticket - this will also stop it from closing.

You can always open a new ticket and refer to this one here by linking to it, we will then know what things are about and be able to reply.

#1352437

Please keep the ticket open till I solve the issue.

#1352487

OK 🙂