按帖子标题或元标题值进行搜索筛选

时间:2018-08-26 作者:Nimesh Deo

我试图通过它的后标题或元标题值来搜索电影。

但它不起作用,它只是通过元值进行搜索。

我需要通过帖子标题搜索,以及i.e 检查标题中是否有关键字。

请不要将我的问题标记为重复,请查看我的代码,然后请帮助我。

/* Live Search
     ========================================================
     */
     if( ! function_exists( \'dooplay_live_search\' ) ) {
        function dooplay_live_search( $request_data ) {
              $parameters = $request_data->get_params();
            $keyword    = dt_clear($parameters[\'keyword\']);
            $nonce      = dt_clear($parameters[\'nonce\']);
           $types      = array(\'movies\',\'tvshows\');
           if( !dooplay_verify_nonce(\'dooplay-search-nonce\', $nonce ) ) return array(\'error\' => \'no_verify_nonce\', \'title\' => __d(\'No data nonce\') );
           if( !isset( $keyword ) || empty($keyword) ) return array(\'error\' => \'no_parameter_given\');
           if( strlen( $keyword ) <= 2 ) return array(\'error\' => \'keyword_not_long_enough\', \'title\' => false );

           $meta_query = array();
           $args = array();
           $search_string =  "test";



           $meta_query[] = array(
              \'key\' => \'original_title\',
              \'value\' => $keyword,
              \'compare\' => \'LIKE\'
           );

           $meta_query[] = array(
              \'key\' => \'original_name\',
              \'value\' => $keyword,
              \'compare\' => \'LIKE\'
           );

           //if there is more than one meta query \'or\' them
           if(count($meta_query) > 1) {
              $meta_query[\'relation\'] = \'OR\';
           }

           // The Query
           $args[\'post_type\'] = $types;
           $args[\'_meta_or_title\'] = $search_string; //not using \'s\' anymore
           $args[\'meta_query\'] = $meta_query;
            $query = new WP_Query( $args );
            if ( $query->have_posts() ) {
              $data = array();
                while ( $query->have_posts() ) {
                    $query->the_post();
                    global $post;
                    $data[$post->ID][\'title\'] = $post->post_title;
                    $data[$post->ID][\'url\'] = get_the_permalink();
                 if ( has_post_thumbnail() ) {
                    $data[$post->ID][\'img\'] = get_the_post_thumbnail_url($post->ID, \'dt_poster_b\');
                 } elseif ($dato = dt_get_meta(\'dt_poster\')) {
                    $data[$post->ID][\'img\'] = dt_image_search(\'dt_poster\', $post->ID, \'w92\', false, true );
                 } else {
                    $data[$post->ID][\'img\'] = esc_url( DOO_URI ) . \'/assets/img/no/poster-small.png\';
                 }
                 if($dato = dt_get_meta(\'release_date\')) {
                    $data[$post->ID][\'extra\'][\'date\'] = substr($dato, 0, 4);
                 }
                 if($dato = dt_get_meta(\'first_air_date\')) {
                    $data[$post->ID][\'extra\'][\'date\'] = substr($dato, 0, 4);
                 }
                 $data[$post->ID][\'extra\'][\'imdb\'] = dt_get_meta(\'imdbRating\');
                }
                return $data;
            } else {
              return array(\'error\' => \'no_posts\', \'title\' => __d(\'No results\') );
            }
            wp_reset_postdata();
        }
     }

1 个回复
SO网友:nmr

在我看来,您似乎没有更改附加到get_meta_sql 过滤器
如果$search_string 是标题的唯一片段,您应该更改post_title = \'%s\' 在源代码中this 答复:

$sql[\'where\'] = sprintf(" AND ( %s OR %s ) ",
        // --- post title begins with the searched phrase --
        $wpdb->prepare( "{$wpdb->posts}.post_title LIKE \'%s\'", $title . \'%\' ),
        // -- OR post title contains the searched phrase --
        // $wpdb->prepare( "{$wpdb->posts}.post_title LIKE \'%s\'", \'%\'. $title . \'%\' ),
        mb_substr( $sql[\'where\'], 5, mb_strlen( $sql[\'where\'] ) )
);

结束