Reputation: 1159
I need help to make a query to customize the default wordpress searching. I don't want to include title or content or excerpt in wordpress searching but want to search based on meta value of that post.
In default wordpress search wordpress add meta query in "AND" Condition. If there is a way if meta query add in or condition it will also be fine.
Please Help
Upvotes: 1
Views: 5786
Reputation: 63
if ( $query->is_search()) {
$meta_query = $query->get( 'meta_query' );
//print_r($meta_query);
$meta_query[] = array('key' => '_product_attributes', /* Product Attribute Meta key Here example (_sku, _stock, _stock_status, height, width ) */
'value' => $city,
'compare' => 'LIKE');
$query->set( 'meta_query', $meta_query );
}
}
}
add_action( 'woocommerce_product_query' , 'Add_custom_search2' );
Upvotes: 0
Reputation: 818
add_action( 'pre_get_posts', function( $q )
{
if( $title = $q->get( '_meta_or_title' ) )
{
add_filter( 'get_meta_sql', function( $sql ) use ( $title )
{
global $wpdb;
// Only run once:
static $nr = 0;
if( 0 != $nr++ ) return $sql;
// Modified WHERE
$sql['where'] = sprintf(
" AND ( %s OR %s ) ",
$wpdb->prepare( "{$wpdb->posts}.post_title like '%%%s%%'", $title),
mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) )
);
return $sql;
});
}
});
$meta_query = array();
$args = array();
$search_string = "test";
$meta_query[] = array(
'key' => 'staff_name',
'value' => $search_string,
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'staff_email',
'value' => $search_string,
'compare' => 'LIKE'
);
//if there is more than one meta query 'or' them
if(count($meta_query) > 1) {
$meta_query['relation'] = 'OR';
}
// The Query
$args['post_type'] = "staff";
$args['_meta_or_title'] = $search_string; //not using 's' anymore
$args['meta_query'] = $meta_query;
$the_query = new WP_Query($args)
Upvotes: 0
Reputation: 187
Here is the code which you need to add in functions.php
This code will work with Post Meta and Taxonomy both
if(!function_exists('custom_wp_meta_and_tax_search')){
function custom_wp_meta_and_tax_search($search_args){
/* taxonomy query and meta query arrays */
$tax_query = array();
$meta_query = array();
/* Keyword Based Search */
if( isset ( $_GET['keyword'] ) ) {
$keyword = trim( $_GET['keyword'] );
if ( ! empty( $keyword ) ) {
$search_args['s'] = $keyword;
}
}
/* Custom Meta Key Parameter */
if( isset($_GET['meta_input']) && ($_GET['meta_input'] != 'any')){
$meta_query[] = array(
'key' => 'custom_meta_key',
'value' => $_GET['meta_input'],
'compare' => 'like',
);
}
$min_value = '';
$max_value = '';
if(isset($_GET['min-value']) && $_GET['min-value'] <> 'any'){
$min_value = $_GET['min-value'];
}
if(isset($_GET['max-value']) && $_GET['max-value'] <> 'any'){
$max_value = $_GET['max-value'];
}
/* Logic for Min and Max value Parameters */
/* You need to replace custom_tax with custom taxonomy */
if( isset($min_value) && ($min_value != 'any') && isset($max_value) && ($max_value != 'any') ){
$min_value = doubleval($min_value);
$max_value = doubleval($max_value);
if( $min_value >= 0 && $max_value > $min_value ){
$meta_query[] = array(
'key' => 'custom_tax',
'value' => array( $min_value, $max_value ),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
);
}
}else if( isset($min_value) && ($min_value != 'any') ){
$min_value = doubleval($min_value);
if( $min_value > 0 ){
$meta_query[] = array(
'key' => 'custom_tax',
'value' => $min_value,
'type' => 'NUMERIC',
'compare' => '>='
);
}
}else if( isset($max_value) && ($max_value != 'any') ){
$max_value = doubleval($max_value);
if( $max_value > 0 ){
$meta_query[] = array(
'key' => 'custom_tax',
'value' => $max_value,
'type' => 'NUMERIC',
'compare' => '<='
);
}
}
// /* if more than one taxonomies exist then specify the relation */
$tax_count = count( $tax_query );
if( $tax_count > 1 ){
$tax_query['relation'] = 'AND';
}
/* if more than one meta query elements exist then specify the relation */
$meta_count = count( $meta_query );
if( $meta_count > 1 ){
$meta_query['relation'] = 'AND';
}
if( $tax_count > 0 ){
$search_args['tax_query'] = $tax_query;
}
/* if meta query has some values then add it to base home page query */
$search_args['meta_query'] = $meta_query;
/* Sort By meta value */
/* You need to replace "custom_meta_key" with the key of custom field you created with post. */
if( (isset($min_value) && ($min_value != 'any')) || ( isset($max_value) && ($max_value != 'any') ) ){
$search_args['orderby'] = 'meta_value_num';
$search_args['meta_key'] = 'custom_meta_key';
$search_args['order'] = 'ASC';
}
return $search_args;
}
}
add_filter('custom_search_parameters','custom_wp_meta_and_tax_search');
Now Run the Query and Filter your posts with Post Meta and taxonomy
$custom_search_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'paged' => $paged,
);
$custom_search_args = apply_filters('custom_search_parameters',$custom_search_args);
$search_query = new WP_Query( $custom_search_args );
Upvotes: 0
Reputation: 15620
If you're referring to changing the main query on search results pages (e.g. http://example.com/?s=hello
), then try one of these options:
Option #1
This is for:
I don't want to include title or content or excerpt in wordpress searching
Hook to pre_get_posts
and empty the s
parameter.
add_action( 'pre_get_posts', function( $wp_query ){
if ( $wp_query->is_main_query() && $wp_query->is_search() ) {
$wp_query->set( 's', '' );
}
});
Option #2
This is for:
In default wordpress search wordpress add meta query in "AND" Condition. If there is a way if meta query add in or condition it will also be fine.
Hook to get_meta_sql
and substitute the AND
with OR
.
add_filter( 'get_meta_sql', function( $sql, $meta_query, $type, $primary_table ){
global $wpdb;
if (
'post' === $type &&
$wpdb->posts === $primary_table &&
is_main_query() && is_search()
) {
if ( $sql['where'] && ' AND (' === substr( $sql['where'], 0, 6 ) ) {
$sql['where'] = ' OR ' . substr( $sql['where'], 5 );
}
}
return $sql;
}, 10, 4 );
Option #3
Create a custom search page and use a custom WP_Query
instance for querying the posts.
$q = new WP_Query([
'meta_key' => 'some_key',
'meta_value' => 'some_value',
// Or use meta_query
]);
Options #1 and #2 are both tricky and may conflict with plugins on your site, or any custom code which modifies the search-related parameters/queries. Or even standard queries. So use the code at your very own risks.
Hope that helps..
Upvotes: 1
Reputation: 818
I create a ajax from where I send the input text as "search_text" and check the text is feature or not from meta_key "job_is_featured" value which is store in postmeta. and also check into custom post type category id.
$catids = '22';
$args = array(
'post_type' => 'job', 'order' => 'DESC','posts_per_page' => '-1','s' => 'search_text' ,'orderby' => 'meta_value',
'tax_query' => array(
array(
'taxonomy' => 'job-category',
'field' => 'term_id',
'terms' => $catids,
),
),
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'job_is_featured',
'value' => 1,
'compare' => '=',
),
array(
'key' => 'job_is_featured',
'value' => 0,
'compare' => '=',
),
),
array(
'key' => 'job_location',
'value' => 'search_text',
'compare' => 'LIKE',
),
),
);
$the_query = new WP_Query( $args );
Upvotes: 0