首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用标签从特定的post类型中搜索?

如何使用标签从特定的post类型中搜索?
EN

WordPress Development用户
提问于 2019-05-29 08:16:32
回答 2查看 2.1K关注 0票数 0

更新:

我有一个定制邮政类型广告。这个帖子类型有一个标题和一个视频id。我想从这个特定的帖子类型中搜索。现在,它返回的结果,只匹配标题,我也希望它返回的结果从标签也。例如,假设我有一个名为"<#>Lorem Ipsum Commercial“的广告,它有一个标记my标记。如果搜索的值与标题或标记匹配,我希望它返回结果。这是我所能得到的。

function.php

代码语言:javascript
复制
function searchCommercial($query) {
  if ( $query -> is_search && !is_admin() ) {
    if( isset($_GET['post_type']) ) {
      $type = $_GET['post_type'];
      if( $type == 'commercials' ) {
        $query -> set( 'post_type', array('commercials') );
        /*
        $terms = explode(' ', $query->get('s'));
        $query->set('tax_query', array(
            'relation'=>'OR',
            array(
                'taxonomy'=>'post_tag',
                'field'=>'slug',
                'terms'=>$terms
            )
        ));*/
      }
    }
  }
  return $query;
}

add_filter( 'pre_get_posts', 'searchCommercial' );

搜寻表格:

代码语言:javascript
复制

search.php

代码语言:javascript
复制
if ( have_posts() ) :
  while ( have_posts() ) : the_post();
    if ( isset($_GET['post_type']) ) {
      $type = $_GET['post_type'];
      if ( $type == 'commercials' ) {
        get_template_part( 'templates/content-commercilas' );
      }
    }
  endwhile;
endif;

我已经试着解决这个问题两天了,任何帮助都会非常感谢。

更新 post类型代码

代码语言:javascript
复制
add_action('init', registering_commercial);
function registering_commercial() {
  $labels = array(
        'name'               => __( 'Commercials' ),
        'singular_name'      => __( 'Commercial' ),
        'menu_name'          => __( 'Commercials' ),
        'name_admin_bar'     => __( 'Commercial' ),
        'add_new'            => __( 'Add New' ),
        'add_new_item'       => __( 'Add New Commercial' ),
        'new_item'           => __( 'New Commercial' ),
        'edit_item'          => __( 'Edit Commercial' ),
        'view_item'          => __( 'View Commercial' ),
        'view_items'         => __( 'View Commercials' ),
        'all_items'          => __( 'All Commercials' ),
        'search_items'       => __( 'Search Commercial' ),
        'parent_item_colon'  => __( 'Parent Commercial:' ),
        'not_found'          => __( 'No Commercial found.' ),
        'not_found_in_trash' => __( 'No Commercial found in Trash.' ),
    'item_published'     => __( 'Commercial is published.' ),
    'item_updated'       => __( 'Commercial successfully updated!' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description for Commercials.' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'commercials' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'thumbnail' ),
    'taxonomies'         => array( 'commercial_category', 'post_tag' )
    );

  register_post_type( 'commercials', $args );
}

类别代码:

代码语言:javascript
复制
add_action( 'init', 'create_commercial_category', 0 );

function create_commercial_category() {
    $labels = array(
        'name'              => __( 'Commercial Categories' ),
        'singular_name'     => __( 'Category' ),
        'search_items'      => __( 'Search Category' ),
        'all_items'         => __( 'All Categories' ),
        'parent_item'       => __( 'Parent Category' ),
        'parent_item_colon' => __( 'Parent Category:' ),
        'edit_item'         => __( 'Edit Category' ),
        'update_item'       => __( 'Update Category' ),
        'add_new_item'      => __( 'Add New Category' ),
        'new_item_name'     => __( 'New Category Name' ),
        'menu_name'         => __( 'Commercial Category' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'commercial-category' )
    );
    register_taxonomy( 'commercial_category', array( 'commercials' ), $args );
}
EN

回答 2

WordPress Development用户

发布于 2019-05-29 17:48:45

自定义查询的方式,您告诉它搜索也符合自定义tax_query条件的帖子。从逻辑上讲,这是行不通的。

您说,“如果搜索了标记,或者它与标题匹配,则返回一个结果。”

我将向您展示一种完全不同的方法,主要使用以下逻辑:

  1. 您说过“这个post类型有一个标题和一个视频id”,所以我假设您不会将字段/列post_excerpt用于CPT commercials。您可以将它添加到supports中,但这并不是工作所必需的。
  2. 我们可以使用action save_post强制WordPress将您的帖子标签添加到广告的post_excerpt字段中。当您查看代码时,这听起来不那么令人困惑。
  3. 我们知道,默认情况下,当查询var WordPress存在时,WordPress会在post_titlepost_excerptpost_content列中搜索一个术语。因此,上面的第2条将解决您的问题,因为WordPress将处理其余的问题。

把我们的手弄脏了。我们仍然需要使用pre_get_posts操作(是的,不是过滤器):

代码语言:javascript
复制
/**
 * @param WP_Query $query
 */
function searchCommercial( $query ) {
    if ( is_search() && ! is_admin() ) {
        if ( isset( $_GET['post_type'] ) ) {
            $type = $_GET['post_type'];
            if ( $type == 'commercials' ) {
                $query->set( 'post_type', array( 'commercials' ) );
            }
        }
    }
}

add_action( 'pre_get_posts', 'searchCommercial' );

以及保存商业post_excerpt中的post标记:

代码语言:javascript
复制
/**
 * Fires once a post has been created or updated.
 *
 * @param int     $post_id
 * @param WP_Post $post
 * @param bool    $update Whether this is an existing post being updated or not.
 */
function add_post_tags_to_commercials( $post_id, $post, $update ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    if ( wp_is_post_revision( $post_id ) ) {
        return;
    }

    if ( $post->post_type != 'commercials' ) {
        return;
    }

    $post_tags = wp_get_object_terms( $post_id, 'post_tag' );

    if ( $post_tags ) {
        $tags_with_comma = [];

        /**
         * @var WP_Term $tag
         */
        foreach ( $post_tags as $tag ) {
            $tags_with_comma[] = $tag->name;
            $tags_with_comma[] = $tag->slug;
        }

        $tags_with_comma = implode( ',', $tags_with_comma );

        // Unhook this function so it doesn't loop infinitely.
        remove_action( 'save_post', 'add_post_tags_to_commercials', 20 );

        wp_update_post(
            array(
                'ID'           => $post_id,
                'post_excerpt' => $tags_with_comma, // The tags will be saved within the `post_excerpt` column.
            )
        );

        // Re-hook this function.
        add_action( 'save_post', 'add_post_tags_to_commercials', 20, 3 );
    }
}

add_action( 'save_post', 'add_post_tags_to_commercials', 20, 3 );

上面的两个片段可以放在functions.php s.php文件中。

通知:

  • 如果您使用Guttenberg作为CPT commercials,则可能会出现附加post标记的问题。到目前为止,WP版本5.2.1仍然在使用AJAX过程将标记附加到post上有困难。
  • 你必须再次保存所有广告。我希望只是几个。
  • 考虑将字符串excerpt添加到commercials supports中,这样可以使您更容易查看添加到post_excerpt列中的内容:
代码语言:javascript
复制
'supports'           => array( 'title', 'thumbnail', `excerpt` ),
票数 1
EN

WordPress Development用户

发布于 2019-05-29 11:54:23

我觉得你需要移除

代码语言:javascript
复制
$_GET['post_type']

请使用下面的代码,我认为您可能会有帮助:

代码语言:javascript
复制
add_action( 'pre_get_posts', function ( $q )
{
    if (    !is_admin()         // Only target front end,
         && $q->is_main_query() // Only target the main query
         && $q->is_search()     // Only target the search page
    ) {
        $q->set( 'post_type', ['commercials'] );

        $terms = explode(' ', $q->get('s'));

        $q->set('tax_query', array(
            'relation'=>'OR',
            array(
                'taxonomy'=>'post_tag',
                'field'=>'slug',
                'terms'=>$terms
            )
        ));
    }
});
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/339052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档