Support

Account

Home Forums Backend Issues (wp-admin) Relation field search in admin

Helping

Relation field search in admin

  • Hello Team,

    I want to get the details from search in admin panel, All field result was came but the relation filed not showing the exact result, this is my code can you help on this

    `function theme_post_search_join( $join ){
    global $pagenow, $wpdb;
    if ( is_admin() && $pagenow == ‘edit.php’ && ! empty( $_GET[‘post_type’] ) && $_GET[‘post_type’] == ‘successstories’ && ! empty( $_GET[‘s’] ) ) {
    $join .= ‘LEFT JOIN ‘ . $wpdb->postmeta . ‘ ON ‘ . $wpdb->posts . ‘.ID = ‘ . $wpdb->postmeta . ‘.post_id ‘;
    }
    return $join;
    }
    add_filter( ‘posts_join’, ‘theme_post_search_join’ );

    function theme_search_where( $where ){
    global $pagenow, $wpdb;
    if ( is_admin() && $pagenow == ‘edit.php’ && ! empty( $_GET[‘post_type’] ) && $_GET[‘post_type’] == ‘successstories’ && ! 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;
    }
    add_filter( ‘posts_where’, ‘theme_search_where’ );

  • You have 2 problems.

    1) The relationship field stores an array

    2) The array is an array of related post IDs and does not store the title of the post.

    It is impossible to search for a title and get posts that have a relationship with another post with searched title. To do this the title of the related post would need to be stored in a meta field of the post where the relationship exists.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Relation field search in admin’ is closed to new replies.