Support

Account

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

Solved

Field / SubField Searching within Admin Panel

  • I have a custom “course code” that I need my administrators to be able to type into the search bar and instantly find. I have these stored as field “course_number” and within a repeater “instance” with “instance_id”

    These codes are showing on my custom column, Columnshttp://i.imgur.com/kyhtibF.jpg

    but not when searched directly Nothing shows uphttp://i.imgur.com/lhcn55P.jpg

    How do I add a filter that can tell WordPress to search on these custom fields?

  • i cant say how you add this with a filter.
    but i can offer a workaround if you dont use core content-WP-wysiwyg (anyway or are able to dont use it.)
    if you meet this requirement than look at my post inside this thread
    with that “workaround” you should be able to find everything that you save like that with core-search, because you use searchable core as hidden placeholder. πŸ˜‰

  • 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' );
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.