最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WordPress Admin Panel search posts with custom post meta values along with title

programmeradmin1浏览0评论

Trying to modify default admin search by including search by custom post type fields,

Below is my code,

function custom_search_query( $query ) {
    $custom_fields = array(
        // put all the meta fields you want to search for here
        "rg_1job_designation",
        "rg_2job_designation"
    );
    $searchterm = $query->query_vars['s'];
    // we have to remove the "s" parameter from the query, because it will prevent the posts from being found
    $query->query_vars['s'] = "";
    if ($searchterm != "") {
        $meta_query = array('relation' => 'OR');
        foreach($custom_fields as $cf) {
            array_push($meta_query, array(
                'key' => $cf,
                'value' => $searchterm,
                'compare' => 'LIKE'
            ));
        }
        $query->set("meta_query", $meta_query);
    };
}
add_filter( "pre_get_posts", "custom_search_query");

But it's not working.

i have a post title -> John & Designation -> Designer On search by Designer can get 1 result, But on search of John result is empty (This should also fetch one result). Now the default search for title is lost want to use that too.

Did anyone know what's wrong in my code?

Trying to modify default admin search by including search by custom post type fields,

Below is my code,

function custom_search_query( $query ) {
    $custom_fields = array(
        // put all the meta fields you want to search for here
        "rg_1job_designation",
        "rg_2job_designation"
    );
    $searchterm = $query->query_vars['s'];
    // we have to remove the "s" parameter from the query, because it will prevent the posts from being found
    $query->query_vars['s'] = "";
    if ($searchterm != "") {
        $meta_query = array('relation' => 'OR');
        foreach($custom_fields as $cf) {
            array_push($meta_query, array(
                'key' => $cf,
                'value' => $searchterm,
                'compare' => 'LIKE'
            ));
        }
        $query->set("meta_query", $meta_query);
    };
}
add_filter( "pre_get_posts", "custom_search_query");

But it's not working.

i have a post title -> John & Designation -> Designer On search by Designer can get 1 result, But on search of John result is empty (This should also fetch one result). Now the default search for title is lost want to use that too.

Did anyone know what's wrong in my code?

Share Improve this question edited Jun 28, 2016 at 6:55 kaiser 50.9k27 gold badges150 silver badges245 bronze badges asked Jan 31, 2014 at 6:26 Vin_fugenVin_fugen 2193 silver badges5 bronze badges 2
  • I realize my answer is not helping, so deleted it. Your problem is something I've came across before. Please refer to this post, I use their code to the rescue. – 1fixdotio Commented Jan 31, 2014 at 8:07
  • Use above edited codes now i can able to filter by meta_value. But now default search for title gets lost. Any idea to use both ? – Vin_fugen Commented Jan 31, 2014 at 10:42
Add a comment  | 

1 Answer 1

Reset to default 5

With this code you can search in post list in WordPress Admin Panel with custom post meta values along with title and other default fields.

Please, add below code in functions.php file:

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;
    }
}
发布评论

评论列表(0)

  1. 暂无评论