更新:
我有一个定制邮政类型广告。这个帖子类型有一个标题和一个视频id。我想从这个特定的帖子类型中搜索。现在,它返回的结果,只匹配标题,我也希望它返回的结果从标签也。例如,假设我有一个名为"<#>Lorem Ipsum Commercial“的广告,它有一个标记my标记。如果搜索的值与标题或标记匹配,我希望它返回结果。这是我所能得到的。
function.php
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' );搜寻表格:
search.php
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类型代码
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 );
}类别代码:
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 );
}发布于 2019-05-29 17:48:45
自定义查询的方式,您告诉它搜索也符合自定义tax_query条件的帖子。从逻辑上讲,这是行不通的。
您说,“如果搜索了标记,或者它与标题匹配,则返回一个结果。”
我将向您展示一种完全不同的方法,主要使用以下逻辑:
post_excerpt用于CPT commercials。您可以将它添加到supports中,但这并不是工作所必需的。save_post强制WordPress将您的帖子标签添加到广告的post_excerpt字段中。当您查看代码时,这听起来不那么令人困惑。post_title、post_excerpt和post_content列中搜索一个术语。因此,上面的第2条将解决您的问题,因为WordPress将处理其余的问题。把我们的手弄脏了。我们仍然需要使用pre_get_posts操作(是的,不是过滤器):
/**
* @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标记:
/**
* 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文件中。
通知:
commercials,则可能会出现附加post标记的问题。到目前为止,WP版本5.2.1仍然在使用AJAX过程将标记附加到post上有困难。excerpt添加到commercials supports中,这样可以使您更容易查看添加到post_excerpt列中的内容:'supports' => array( 'title', 'thumbnail', `excerpt` ),发布于 2019-05-29 11:54:23
我觉得你需要移除
$_GET['post_type']请使用下面的代码,我认为您可能会有帮助:
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
)
));
}
});https://wordpress.stackexchange.com/questions/339052
复制相似问题