Skip Navigation

[Resolved] Provide search in edit list trough relationship posts post_title

This support ticket is created 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by Wilhem 3 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#2192937

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

#2193567

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

#2194575

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

#2195547

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

#2196917

My issue is resolved now.
Thank you!
Best Wilhem