将搜索结果限制为完全匹配

时间:2012-08-06 作者:Jure Mali

我在Wordpress中显示结果时遇到问题WP_Query. 这个问题很简单,但我不是程序员,所以很难解决。

因此,我有一个自定义字段,其值如下1-1-1-12-1-1-1-1 例如例如,当我搜索时1-1-1-1 这也给了我第二个结果,这是我不想看到的。我只想在搜索结果中显示精确匹配。有人能帮我吗?

代码:

    <?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
        global $wp_query;

        $query = new WP_Query( \'meta_key=wpcf-resitevtags\' );
        $query = $wp_query->query;
        if (!is_array($query)) parse_str($query, $query); 
        $custom_query = new WP_Query(array_merge($query, array(\'posts_per_page\' => $gantry->get(\'search-count\'), \'meta_key\' => \'wpcf-resitevtags\', \'posts_per_page\' => 22,  \'paged\' => $paged)));
         ?>

        <?php if($custom_query->have_posts()) : ?>

        <?php if($gantry->get(\'search-page-title\')) : 

        ?>
        <?php endif; ?>

        <?php while ($custom_query->have_posts()) : $custom_query->the_post(); ?>

        <?php

        //$title    = strip_tags(get_the_title());
        $content    = strip_tags(strip_shortcodes(get_the_content(false)));
        $excerpt    = strip_tags(strip_shortcodes(get_the_excerpt()));
        $keys       = explode(" ",$s);
        //$title    = preg_replace(\'/(\'.implode(\'|\', $keys) .\')/iu\', \'<span class="search-excerpt">\\0</span>\', $title);
        //$content  = preg_replace(\'/(\'.implode(\'|\', $keys) .\')/iu\', \'<span class="search-excerpt">\\0</span>\', $content);
        //$excerpt  = preg_replace(\'/(\'.implode(\'|\', $keys) .\')/iu\', \'<span class="search-excerpt">\\0</span>\', $excerpt);

        ?>

1 个回复
最合适的回答,由SO网友:Rutwick Gangurde 整理而成

我想是meta_query 应该对你有帮助。

$query = new WP_Query( array(array(
                                    \'key\' => \'wpcf-resitevtags\',
                                    \'value\' => \'1-1-1-1\' 
                                    \'compare\' => \'=\'
                            ))
                    );

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register