Hi team,
I'm not able to find a post which have a relationship to a listed post.
Example :
"Projections" have a relation to films projected
When I fill "Mother" in search field ( edit.php )
I need to find all projections which have the film "Mother" connected to that projection.
add_filter( 'posts_where', 'projections_search_where' );
function projections_search_where( $where ) {
global $pagenow, $wpdb;
...
}
Thanks for your help !
Wilhem
Hello,
There isn't such kind of built-in feature within Toolset plugins.
As a workaround, you can try these:
1) In the custom search form, display a relationship field as a select field:
https://toolset.com/course-lesson/creating-a-custom-search/#how-to-search-by-post-relationships
2) Use custom JS codes to change above select field as a select2 input box:
See the solution in below related thread:
https://toolset.com/forums/topic/split-how-to-convert-a-normal-dropdown-to-a-select2-dropdown/#post-1207742
Hi Luo,
Thank you for your reply.
But unfortunately, none of that solutions would work. You replied me by frontend solution ; I need a solution for the backend ( edit.php as I said ).
Furthermore, I already add a select in edit filters ( just before table ) with "Films". What I need is to provide a correct search trough post_type and relationship post_type with the search input ( on the right of the filters ) "s".
Meaning, if you have a "Projections" named "Mother" and a "Film related to a Projection" named "Nature" ; if you fill "Mother Nature" in search input, you will be able to find both "Projections" posts in list.
Is there a function to find a relationship by title instead by id ?
Do I need to make a custom Query ?
Can you help me with that / i believe there a must need for all Types users.
Normally i use that hook to extend search to custom meta fields for example
function projections_search_where( $where ) {
global $pagenow, $wpdb;
// I want the filter only when performing a search on edit page of Custom Post Type
if ( is_admin() && is_search() && 'edit.php' === $pagenow && 'projection' === $_GET['post_type'] && ! empty( $_GET['s'] ) ) {
$where = preg_replace(
"/\(\s*" . $wpdb->posts . ".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(" . $wpdb->posts . ".post_title LIKE $1) OR (" . $wpdb->postmeta . ".meta_value LIKE $1)", $where );
return $where;
}
Thanks a lot,
Wilhem
There isn't such kind of built-in feature within Toolset Types plugin, you might consider custom codes:
1) Use the URL parameter $_GET['s'] to search the "film" posts:
https://developer.wordpress.org/reference/classes/wp_query/#search-parameters
2) Use these "film" post IDs to get related "Projections" posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
My issue is resolved now.
Thank you!
Best Wilhem