Support

Account

Home Forums Backend Issues (wp-admin) Field / SubField Searching within Admin Panel Reply To: Field / SubField Searching within Admin Panel

  • This certainly can work but I’d like to keep the content area as-is for excerpt functionality. I did find a solution but it looks like it searches all post_meta, so it’s not something that can specifically be set.

    I would like to see a “Searchable” Boolean added to field types.

    // Search on custom fields
    function custom_search_join ($join){
        global $pagenow, $wpdb;
        $types = ['course'];
        // I want the filter only when performing a search on edit page of Custom Post Type in $types array
        if ( is_admin() && $pagenow=='edit.php' && in_array( $_GET['post_type'], $types ) && isset( $_GET['s'] ) ) {    
            $join .='LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
        }
        return $join;
    }
    add_filter('posts_join', 'custom_search_join' );
    function custom_search_where( $where ){
        global $pagenow, $wpdb;
        $types = ['course'];
        // I want the filter only when performing a search on edit page of Custom Post Type in $types array
        if ( is_admin() && $pagenow=='edit.php' && in_array( $_GET['post_type'], $types ) && isset( $_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', 'custom_search_where' );
    function custom_search_distinct( $where ){
        global $pagenow, $wpdb;
        $types = ['course'];
        if ( is_admin() && $pagenow=='edit.php' && in_array( $_GET['post_type'], $types ) && isset( $_GET['s'] ) ) {
        return "DISTINCT";
    
        }
        return $where;
    }
    add_filter( 'posts_distinct', 'custom_search_distinct' );