【问题标题】:Search for Custom Field with Custom Post Types使用自定义帖子类型搜索自定义字段
【发布时间】:2011-10-23 19:22:42
【问题描述】:

有一些教程解释了如何将搜索限制为特定类别。

我的问题是,有没有办法将 wordpress 的搜索配置为在自定义帖子类型中搜索自定义字段值。

例如,如果我搜索“你好”,结果会出现具有某个自定义字段等于“你好”的帖子。特定的帖子也将是特定的自定义帖子类型。

感谢任何帮助。

【问题讨论】:

    标签: wordpress search custom-post-type custom-fields


    【解决方案1】:

    要按自定义帖子类型过滤搜索,请使用:

    <?php query_posts($query_string . '&post_type=custom-post-type-name' ); ?>
    

    在循环之前..然后在循环内添加类似的条件

    <?php if ($meta_data[ 'meta-name' ] == 'hello') {
        //do something
    } ?>
    

    【讨论】:

      【解决方案2】:

      我想这就是你要找的东西:

      key 是自定义字段。 value 是您要查找的值 compare 是您要使用的运算符。 如果你愿意,你也可以使用 LIKE。

      // WP_Query arguments
      $args = array (
          'post_type'              => 'vendors',
          'post_status'            => 'published',
          'meta_query'             => array(
              array(
                  'key'       => 'state',
                  'value'     => 'Misissipi',
                  'compare'   => '=',
              ),
          ),
      );
      
      // The Query
      $query = new WP_Query( $args );
      
      // The Loop
      if ( $query->have_posts() ) {
          while ( $query->have_posts() ) {
              $query->the_post();
              // do something
          }
      } else {
          // no posts found
      }
      
      // Restore original Post Data
      wp_reset_postdata();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多