我正在根据表单中的用户条目进行查询,以从我的帖子查询中提取帖子。我希望表单保持简单,所以我希望只需要使用一个字段。该字段将显示搜索零件号或产品标题。这将是一个Ajax响应。下面是我现在掌握的代码:
function led_search_function(){
$args = array(
\'post_type\' => \'al_product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 20,
);
// for taxonomies / categories
if( isset( $_POST[\'categoryfilter\'] ) && $_POST[\'categoryfilter\'] )
$args[\'tax_query\'] = array(
array(
\'taxonomy\' => \'product_category\',
\'field\' => \'id\',
\'terms\' => $_POST[\'categoryfilter\']
)
);
// create $args[\'meta_query\'] array if one of the following fields is filled
if( isset( $_POST[\'part_number\'] ) && $_POST[\'part_number\'] )
$args[\'meta_query\'] = array( \'relation\'=>\'OR\' ); // OR means that both below conditions of meta_query should be true
if( isset( $_POST[\'part_number\'] ) && $_POST[\'part_number\'] ) {
//checks for the part number
$args[\'meta_query\'][] = array(
\'key\' => \'part_number\',
\'value\' => $_POST[\'part_number\'],
\'compare\' => \'LIKE\',
);
//checks if variations have part numbers to add to the array
$args[\'meta_query\'][] = array(
\'key\' => \'product_variations_$_part_number\',
\'value\' => $_POST[\'part_number\'],
\'compare\' => \'LIKE\',
);
}
$query = new WP_Query( $args );
if( $query->have_posts() ) :
echo \'<h2>Your Search Results</h2><div class="productCategories grid">\';
while( $query->have_posts() ): $query->the_post(); ?>
<div class="product_cat">
<a href="<?php the_permalink();?>">
<?php the_post_thumbnail(\'small\');?>
<h2><?php the_title();?></h2>
<?php
$subtitle = get_field(\'subtitle\');
if ( $subtitle ) {
echo \'<h3 class="entry-subtitle">\'.$subtitle.\'</h3>\';
}
?>
</a>
</div>
<?php
endwhile;
echo \'</div>\';
wp_reset_postdata();
else :
echo \'No lights found\';
endif;
die();
}
现在有点误导,因为表单上的字段仍然称为;零件号“;即使它会搜索一切。
我希望能够在查询中添加第三部分来搜索帖子标题(或部分标题……因此,如果有人键入“sun”,它将返回名为“sunbeast”或“sun set light”的产品;。
第二个问题是,如果搜索词不在标题中,但仍然是零件号,我需要它返回结果。
我知道我可以使用;s“;变量,但它也搜索内容,并且对标题大小写要求严格。有没有更好的方法?
UPDATE基于@sally cj,我尝试过:(all in my functions.php),但无论我输入什么,它都会返回所有产品。
<?php
//enqueue product filter ajax js
add_action( \'wp_enqueue_scripts\', \'product_finder_scripts\' );
function product_finder_scripts() {
if ( is_front_page() ) {
wp_enqueue_script( \'ajax-filter-script\', get_stylesheet_directory_uri() . \'/js/ajax-filter.js\', array(\'jquery\'), false, true );
}
}
add_action(\'wp_ajax_myfilter\', \'led_search_function\');
add_action(\'wp_ajax_nopriv_myfilter\', \'led_search_function\');
function my_posts_where( $where ) {
$where = str_replace("meta_key = \'product_variations_$", "meta_key LIKE \'product_variations_%", $where);
return $where;
}
//add_filter(\'posts_where\', \'my_posts_where\');
function led_search_function(){
$args = array(
\'post_type\' => \'al_product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 20,
);
// for taxonomies / categories
if( isset( $_POST[\'categoryfilter\'] ) && $_POST[\'categoryfilter\'] )
$args[\'tax_query\'] = array(
array(
\'taxonomy\' => \'product_category\',
\'field\' => \'id\',
\'terms\' => $_POST[\'categoryfilter\']
)
);
/**
// create $args[\'meta_query\'] array if one of the following fields is filled
if( isset( $_POST[\'part_number\'] ) && $_POST[\'part_number\'] )
$args[\'meta_query\'] = array( \'relation\'=>\'OR\' ); // OR means that both below conditions of meta_query should be true
if( isset( $_POST[\'part_number\'] ) && $_POST[\'part_number\'] ) {
//checks for the part number
$args[\'meta_query\'][] = array(
\'key\' => \'part_number\',
\'value\' => $_POST[\'part_number\'],
\'compare\' => \'LIKE\',
);
//checks if variations have part numbers to add to the array
$args[\'meta_query\'][] = array(
\'key\' => \'product_variations_$_part_number\',
\'value\' => $_POST[\'part_number\'],
\'compare\' => \'LIKE\',
);
}
**/
add_filter( \'posts_clauses\', \'my_posts_clauses\', 10, 2 );
$query = new WP_Query( $args );
remove_filter( \'posts_clauses\', \'my_posts_clauses\', 10 );
if( $query->have_posts() ) :
echo \'<h2>Your Search Results</h2><div class="productCategories grid">\';
while( $query->have_posts() ): $query->the_post(); ?>
<div class="product_cat">
<a href="<?php the_permalink();?>">
<?php the_post_thumbnail(\'small\');?>
<h2><?php the_title();?></h2>
<?php
$subtitle = get_field(\'subtitle\');
if ( $subtitle ) {
echo \'<h3 class="entry-subtitle">\'.$subtitle.\'</h3>\';
}
?>
</a>
</div>
<?php
endwhile;
echo \'</div>\';
wp_reset_postdata();
else :
echo \'No lights found\';
endif;
die();
}
// In the theme\'s functions.php file, or somewhere in your plugin:
function my_posts_clauses( $clauses, $query ) {
$part_number = $_POST[\'part_number\'] ?? \'\';
if ( strlen( $part_number ) && ! is_admin() ) {
global $wpdb;
$part_number2 = \'%\' . $wpdb->esc_like( $part_number ) . \'%\';
$clauses[\'join\'] .= " INNER JOIN $wpdb->postmeta my_mt1 ON {$wpdb->posts}.ID = my_mt1.post_id";
// Search in the metadata part_number.
$where = $wpdb->prepare( "(my_mt1.meta_key = \'part_number\'
AND my_mt1.meta_value LIKE %s)", $part_number2 );
// Search in the metadata product_variations_$_part_number.
$where .= $wpdb->prepare( " OR (my_mt1.meta_key = \'product_variations_\\$_part_number\'
AND my_mt1.meta_value LIKE %s)", $part_number2 );
// Search in post title.
$where .= $wpdb->prepare( " OR ({$wpdb->posts}.post_title LIKE %s)", $part_number2 );
$clauses[\'where\'] .= " AND ( $where )";
$clauses[\'groupby\'] = "{$wpdb->posts}.ID";
}
return $clauses;
}