I created a Toolset view to display all my CPT list called "Store Locator", the post has a custom field called "Product"(it's not the woocommerce product), which is created by the ACF plugin(See attachment).
I tried to add a search filter, which is for search store locator with a particular product name. But I can't find it in the custom search filter. The custom field slug is called "re-product".
Hi Wilfred,
Thank you for contacting us and I'd be happy to assist.
To suggest the best way to make the search filter work with a custom field created through a third-party plugin (ACF), I'll need to see exactly how it is set up to store the data.
Can you please share temporary admin login details, along with the link to a page with this post view?
Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.
regards,
Waqar
Hi Wilfred,
Thank you for sharing the admin access.
I noticed that the custom field that you're referring to is a "Relationship" type field from the ACF plugin.
Since the ACF plugin stores the value in such fields in a serialized data format, there is no built-in or simple way through which that data can be used with Toolset features, including the search filters in views.
Is there any specific reason, you're using a "relationship" field from ACF plugin for this and not Toolset's own "Post Relationship" feature?
( ref: https://toolset.com/documentation/post-relationships/ )
If you'll use Toolset's post-relationship feature for storing these connections, you'll be able to use it for search filters as well:
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/how-to-filter-posts-by-their-ancestors/
Note: There are a number of plugins on your website for which newer versions are available and it is always recommended to use the up-to-date plugins.
regards,
Waqar
Hi,
I used to be using the toolset relationship for connecting the Store locator and the retailer product CPT, can manage to add the filter in the view.
But we build a mobile app which is connected to the WordPress site database, so I need to create a Rest API for connecting it, when I checking Store locator API JSON, it doesn't have data for assigned retailer product, but ACF have the data, so that why i decide to use ACF for the relationship custom field.
Hi Wilfred,
Thank you for sharing the background information.
In that case, my recommendation would be to keep on using Toolset's post-relationship, as it will allow you to use search filters.
For getting the related posts through REST API, you can register a custom endpoint/route that uses the "toolset_get_related_posts" function.
( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts )
You'll find a good usage example in this reply:
https://toolset.com/forums/topic/writing-rfg-to-cpt-via-rest/#post-1310253
For more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
Hi,
Based on this forum that you give me https://toolset.com/forums/topic/writing-rfg-to-cpt-via-rest/#post-1310253
Can I explain how should I use it?
Thank
function rest_related_posts( $request ){
$parameters = $request->get_query_params();
if ( isset( $parameters['relationship'] ) ) {
$relationship = toolset_get_relationship( $parameters['relationship'] );
if ( $relationship ) {
$parent = $relationship['roles']['parent']['types'][0];
$child = $relationship['roles']['child']['types'][0];
$post = get_post( $request['id'] );
$type = $post->post_type;
$origin = ( $parent == $type ) ? 'parent' : 'child';
// Get connected posts
$connections = toolset_get_related_posts( $post->ID, $parameters['relationship'], array(
'query_by_role' => $origin,
'role_to_return' => 'other',
'return' => 'post_id'
)
);
return implode(',',$connections);
}
}
}
register_rest_route( 'customendpoint/v1', '/related/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'rest_related_posts'
) );
Hi,
As per our support policy, we can guide you around how Toolset Features and functions work.
( ref: https://toolset.com/toolset-support-policy/ )
But for general WordPress, REST API and PHP code customizations, it is recommended to either hire a contractor or consult the specialized community forums:
https://wordpress.org/support/forums/
https://wordpress.stackexchange.com/
Let me briefly explain, what the components in that example code snippet do:
1. "register_rest_route" function:
This WordPress core function registers a new custom REST API route, which can be used in the third-party app.
https://developer.wordpress.org/reference/functions/register_rest_route/
2. "toolset_get_related_posts" function:
This Toolset function can be used to get the list of related/connected posts by specifying the target post, post-relationship, and role in that relationship.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
I hope this makes it more clear.
regards,
Waqar