wp_query搜索结果预计不会

问题描述 投票:0回答:1

我是一个资深IT,而不是一个编码器。我创建一个WordPress的search.php页面到我的子主题。这种特殊的网页,必须从3 CPT返回结果

  • “结构”
  • '游览'
  • “要约”

下面的代码,返回ID的数组来填充被称为“基本电网”与搜索结果中的美格插件引擎,代码工作,但我有以下问题。

  • 如果我搜索“腓力”(这是一个struttura CPT)将返回正确的
  • 如果我搜索“高级”(这是一个offerta CPT)将返回正确的
  • 如果我搜索“亚历山德罗”(即旅游CPT)将返回正确的
  • 如果我搜索“notpresent”是不存在的任何CPT是返回正确的回波串,说再拍搜索
  • 如果我搜索“货物”是存在于“post_type - >”页面”我期待同回声字符串上面,而是返回一些随机(???)CPT职位。

就像是一个错误的方式搜索到网页post_type。任何建议?

这是搜索页面http://neos.anekitalia.com/?s

<?php
/*
Template Name: Search Page
*/
?>
<?php
get_header();
?>
<?php echo do_shortcode('[et_pb_section global_module="216661"][/et_pb_section]'); //show section Vuoi fare un'altra ricerca? ?>
<div class="et_pb_section et_section_regular" style="padding: 0;min-height:250px;">
                        <div class="et_pb_row">
                            <div class="et_pb_column et_pb_column_4_4 et_pb_column_14     et_pb_css_mix_blend_mode_passthrough et-last-child" >
                                <div class="et_pb_module et_pb_code">
                                    <div class="et_pb_code_inner">
<?php // Build Search Query
$args = array(
    's'         => $_REQUEST['s'],
    'showposts' => -1,
    'post_type' => array ( 'tour', 'struttura', 'offerta' ) // define searchable Post Types
);
$tp_allsearch = new WP_Query($args);


$posts = array();
// If there are Search posts
if($tp_allsearch->have_posts()) :

// Go through result
while($tp_allsearch->have_posts()) : $tp_allsearch->the_post();

// Save Post ID in array    
$posts[] = $post->ID;

endwhile;
// Build shortcode with the $post array build before to populate essential grid engine
$the_content = do_shortcode('[ess_grid alias="searchdefault" posts="'.implode(',', $posts).'"]');
// Echo Out the result in your page
echo $the_content;

else:
//show section no results found
   echo do_shortcode('<div style="max-width: 800px; margin: 0 auto"> [et_pb_section global_module="216673"][/et_pb_section]</div>');
endif;
?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
<!--footer and stuffs-->                    
                    <?php echo do_shortcode('[et_pb_section global_module="208275"][/et_pb_section]');?>
                        <?php echo do_shortcode('[et_pb_section global_module="210037"][/et_pb_section]');?>
                    <?php echo do_shortcode('[et_pb_section global_module="210036"][/et_pb_section]');?>
<?php

get_footer();

编辑:

我发现为什么发生这种情况,只不过是我正在寻找存在为在底部,所以我必须找到一个方法来排除文本现在到内容的DIV部分的所有文字,是有办法使查询只进标题和排除的内容?

php wordpress search
1个回答
0
投票

我发现这class-wp-query

$search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );

默认情况下,$search看起来是这样的:

    (wp_posts.post_title LIKE %s) 
    OR (wp_posts.post_excerpt LIKE %s) 
    OR (wp_posts.post_content LIKE %s)
    AND (wp_posts.post_password = '')  

所以它看起来像您可以将过滤器添加到主题functions.php。它相当棘手删除查询的帖子内容和岗位摘录部分与预浸更换或任何但是你可以串操纵它,以便它更象:

    (wp_posts.post_title LIKE %s) 
    OR (wp_posts.post_title LIKE %s) 
    OR (wp_posts.post_title LIKE %s)
    AND (wp_posts.post_password = '') 

通过做这样的事情

add_filter('posts_search', 'so_54550119_search_filter');
function so_54550119_search_filter($search){
   return str_replace(array('excerpt','content'), array('title','title'), $search);
}
© www.soinside.com 2019 - 2024. All rights reserved.