How do I combine these two Wordpress search queries? - php

I have two search queries. One searches in the default manner, for any post titles that match the arguments.
The second query is set to search any posts with the postmeta key of "SKU" LIKE the search query.
I am trying to combine these two queries so that the search will return any posts whose title OR sku match the search term.
First query:
$args = array(
's' => apply_filters('yith_wcas_ajax_search_products_search_query', $search_keyword),
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => apply_filters('yith_wcas_ajax_search_products_posts_per_page', get_option('yith_wcas_posts_per_page')),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
)
);
Second query:
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => apply_filters('yith_wcas_ajax_search_products_posts_per_page', get_option('yith_wcas_posts_per_page')),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
),
array(
'key' => '_sku',
'value' => apply_filters('yith_wcas_ajax_search_products_search_query', $search_keyword),
'compare' => 'LIKE'
)
)
);
How can I combine these two queries, and return any posts with the title or the sku matching the search term?

I'm assuming you will be using args in the loop.
You can use the loop to add all the returned post_ids to an array. You could run two seperate loops, and add all the entries to an array. You would need to check for double entries, so you don't end up printing the same post twice.
So you would do something like-
//First loop
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => apply_filters('yith_wcas_ajax_search_products_posts_per_page', get_option('yith_wcas_posts_per_page')),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
),
)
);
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$my_post = my_post_function($post_id);
//Store the items in an array
$my_post_array [] = $my_post;
query_posts($args);
endwhile;
//Second loop
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => apply_filters('yith_wcas_ajax_search_products_posts_per_page', get_option('yith_wcas_posts_per_page')),
'meta_query' => array(
array(
'key' => '_sku',
'value' => apply_filters('yith_wcas_ajax_search_products_search_query', $search_keyword),
'compare' => 'LIKE'
)
)
);
while ( $loop->have_posts() ) : $loop->the_post();
$post_id = get_the_ID();
$my_post = my_post_function($post_id);
//Store the items in an array
$my_post_array [] = $my_post;
query_posts($args);
endwhile;
//Remove duplicate entries from the array
array_unique ( $my_post_array, SORT_STRING );

Related

woocommerce product attribute, how to filter between two values

Lets say i have a list off houses and they have a attribute called "size" now I want to get all houses between size 200 and 300.
I have tried
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'pa_size',
'value' => array($sizeMin, $sizeMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
);
Then I tried with tax_query but I couldn't find a way to get a term between two values.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'pa_size',
'field' => 'slug',
'terms' => $sizevalue
)
);
);
Can't understand if this should not be possible but I think the value has to be a string therefor it cant be between.
for now im sorting them in my foreach loop when im displaying them but then my pagination is not working.
My conclusion was that you cant do this with woocommerce product attributes because they a text based, så I made some Advancec custom fields and used them insted like this
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND,
array(
'key' => 'myCutsomField',
'value' => array($sizeMin, $sizeMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'myCutsomField2',
'value' => array($valueMin, $valueMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
$products = new WP_Query( $args );

Cannot order destinations alphabetically

I have unordered destinations, which i print in the following way:
$destinations = get_posts( array(
'post_type' => 'destination_showcase',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'destination_state',
'value' => ':"'.$state_id . '";' , // looking for serialized value in quotes
'compare' => 'LIKE'
)
),
) );
I want to print the destinations alphabetically. When i var_dump-ed the #destinations array i got all of the destinations and their parameters. I want to get their titles i.e "post_title" and print it alphaberically. I've tried this, but doesn't work:
'orderby'=> $destinations->post_title, 'order' => 'ASC',
Any ideas how the task can be done ?
just try this
$destinations = get_posts( array(
'post_type' => 'destination_showcase',
'posts_per_page' => -1,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'destination_state',
'value' => ':"'.$state_id . '";' , // looking for serialized value in quotes
'compare' => 'LIKE'
)
),
) );
If I read the WP Query manual properly, this should work:
get_posts( array(
'post_type' => 'destination_showcase',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'destination_state',
'value' => ':"'.$state_id . '";' ,
'compare' => 'LIKE'
)
),
'orderby' => 'title',
'order' => 'ASC',
) );

Count posts with meta value

I'm trying to show the number of posts that have certain meta key values for the current user.
This is my code:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'author' => $current_user_id,
'meta_query' => array(
'key' => 'color',
'value' => array('red', 'blue')
),
);
$posts_array = get_posts( $args );
$the_count = count($posts_array);
echo $the_count;
Thi is counting ALL posts for the current user, ignoring the meta key values.
I only need the $the_count to be the number of posts that have a meta key value 'red' or 'blue' for the current user.
What am I doing wrong?
Thanks in advance!
I am not sure, but you could try something like this:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'author' => $current_user_id,
'meta_key' => 'color',
'meta_value' => array('red', 'blue')
);
$posts_query = new WP_Query($args);
$the_count = $posts_query->post_count;
echo $the_count;
If you want to use the meta_query array, you have to put the meta_key and meta_value in a subarray:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'author' => $current_user_id,
'meta_query' => array(
array(
'key' => 'color',
'value' => array('red', 'blue'),
),
),
);
$posts_array = get_posts( $args );
$the_count = count($posts_array);
This is because you can use multiple meta_key to combine them.
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'color',
'value' => array('red', 'blue'),
),
array(
'key' => 'size',
'value' => array('l', 'xl', 'xxl'),
),
),

Wordpress WP_Query with multiple meta_values for a meta_key

I'm trying to run a WP_Query in order to search for all the products in my database with multiple meta values.
e.g
Product 1 -> meta_key['key1'] ->meta_value['value1']
Product 2 -> meta_key['key1'] ->meta_value['value2']
Product 3 -> meta_key['key1'] ->meta_value['value3']
So i want to get all three products.My arguments are
$args = array(
'post_type' => 'product',
'posts_per_page' => 2,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'key1',
'value' => 'val1',
'compare' => '='
),
array(
'key' => 'key1',
'value' => 'val2',
'compare' => '='
),
array(
'key' => 'key1',
'value' => 'val3',
'compare' => '='
),
),
'paged' => $paged
);
The problem is that no products are returned. Instead , if a give only one meta_key => meta_value pair it works fine
$args = array(
'post_type' => 'product',
'posts_per_page' => 2,
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'key1',
'meta_value' =>'val1',
'paged' => $paged
);
You should use IN as compare value, like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => 2,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'key1',
'value' => array('val1','val2','val3'),
'compare' => 'IN',
),
),
'paged' => $paged
);
Hope it helps!

Order wp_query by numeric custom field

I'm trying to allow users to sort a list of results from a wp_query by a numeric custom field value. Users can click a button to change the sort by values, but when Price is selected, this is what the wp_query looks like:
'meta_key' => 'adult',
'orderby' => 'meta_value_num',
'order' => 'ASC',
The field is the numeric field from ACF (ACF 5 beta is being used) with a step size of 0.01 to allow for prices like 9.99.
However, if I run this query it returns no results every time, even when there are definitely results that should be matching.
Any ideas where I'm going wrong?
EDIT - full query
$keywordString = $_SESSION['search']['keyword'];
$keywords = explode(', ', $keywordString);
$taxQuery = array(
'relation' => 'AND',
array (
'taxonomy' => 'main-cat',
'field' => 'slug',
'terms' => $_SESSION['search']['cat']
)
);
if( $_SESSION['search']['keyword'] != '' ) {
$taxQuery[] = array(
'taxonomy' => 'sub-cat',
'field' => 'name',
'terms' => $keywords
);
}
$args = array(
// general
'post__in' => $postIDs,
'post_type' => 'event',
'posts_per_page' => 10,
'paged' => $paged,
'cache_results' => false,
'meta_key' => $_SESSION['search']['sort-key'], // becomes 'adult'
'orderby' => $_SESSION['search']['sort-by'], // becomes 'meta_value_num'
'order' => 'ASC',
// category filter
'tax_query' => $taxQuery,
// date filter
meta_query' => array(
'relation' => 'AND',
array(
'key' => 'date_%_start-date',
'value' => $when,
'compare' => '>=',
'type' => 'NUMERIC'
),
array (
'key' => 'date_%_end-date',
'value' => $when2,
'compare' => '<=',
'type' => 'NUMERIC'
)
)
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );

Categories