Wednesday, 24 March 2021

Ts

 





if (!function_exists('extend_admin_search')) {

    add_action('admin_init', 'extend_admin_search');


    /**

     * hook the posts search if we're on the admin page for our type

     */

    function extend_admin_search() {

        global $typenow;


        if ($typenow === 'your_custom_post_type') {

            add_filter('posts_search', 'posts_search_custom_post_type', 10, 2);

        }

    }


    /**

     * add query condition for custom meta

     * @param string $search the search string so far

     * @param WP_Query $query

     * @return string

     */

    function posts_search_custom_post_type($search, $query) {

        global $wpdb;


        if ($query->is_main_query() && !empty($query->query['s'])) {

            $sql    = "

            or exists (

                select * from {$wpdb->postmeta} where post_id={$wpdb->posts}.ID

                and meta_key in ('rg_1job_designation','rg_2job_designation')

                and meta_value like %s

            )

        ";

            $like   = '%' . $wpdb->esc_like($query->query['s']) . '%';

            $search = preg_replace("#\({$wpdb->posts}.post_title LIKE [^)]+\)\K#",

                $wpdb->prepare($sql, $like), $search);

        }


        return $search;

    }

}

No comments:

Post a Comment