2

我正在尝试构建和增强默认的 wordpress 搜索,以便您也可以搜索页面和自定义帖子类型元数据。它工作得很好,除了没有输出自定义字段标题。我这样做print_r($custom_fields);了,它表明值已存储。我只是迷失了我所缺少的东西,但我认为这与跳过第一个值的两个 foreach 循环有关?

以下是显示正在发生的事情和代码的图像:

正在搜索的文本 正在搜索的文本

搜索结果:查看 Parkmerced 的方式(第一段): 在此处输入图像描述

搜索结果:我们汇集了(第二段): 在此处输入图像描述

functions.php(与搜索相关的函数)

function cf_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
    }

    return $join;
}
add_filter('posts_join', 'cf_search_join' );

function cf_search_where( $where ) {
    global $pagenow, $wpdb;

    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'cf_search_where' );

function cf_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );

搜索.php

<?php if ( have_posts() ) : ?>
<section class="search-results">
  <p class='show-results'><?php printf( __( 'Showing search results for: <span>%s', 'parkmerced-vision' ), get_search_query() ); ?></span></p>

  <?php while ( have_posts() ) : the_post(); ?>
    <!-- something found  -->


      <div class="result animated fade-in-up delay-<?php echo $i; ?>">
        <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a>
        <?php
          the_excerpt();

          $searched =  get_search_query();
          $custom_fields = get_post_custom();

          foreach($custom_fields as $field_key => $field_values) {
            foreach($field_values as $key => $value) {

              if(stripos($value, $searched)) {

                $in = $value;
                $search = $searched;
                $replace = '<strong>' . $searched . '</strong>';

                $out = str_ireplace($search, $replace, $in);

                echo '<p>' . $out . '</p>';
              }
            }
          }
        ?>
      </div>

  <?php endwhile; else : ?>
    <!-- nothing found  -->

      <h2>Nothing Found</h2>
      <div class="alert alert-info">
        <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>
      </div>
      <a class="search-back btn" href="<?php echo home_url('/'); ?>/faq">return to faq</a>

<?php endif; ?>

</section>


<?php get_footer(); ?>
4

1 回答 1

0

问题不在于发布数据,而在于值本身的输出。数组连接在一起,导致所有字符串的第一个单词附加到先前的数组值:

  $searched =  get_search_query();
  $custom_fields = get_post_custom();

  foreach($custom_fields as $field_key => $field_values) {
    foreach($field_values as $key => $value) {

      if (stripos($value, $searched) !== false) {
        $in = $value;
        $search = $searched;
        $replace = '<strong>' . $searched . '</strong>';
        $out = str_ireplace($search, $replace, $in);
        echo '<p>' . $out . '</p>';
      }
    }
  }
于 2016-05-11T17:39:05.533 回答