How do I efficiently query a post with multiple conditions? - php

I have call-to-action posts that I want to display on my front page. I'd like to query these custom post types and display one of them that has a launch date < today's date, and an expiry date > today's date. If there aren't any that meet these conditions, I want to display a call-to-action that doesn't have an expiry date.
Is there a way I can query all call-to-action posts, then randomly display one that meets the first condition, and then if empty, the second condition?
I've successfully tried querying twice, one for each condition (see below). But I think I'd rather query the database once. As this way I can add further conditions as needed, without making innumerable queries. Or would this approach be ill-advised?
$today = date( "Ymd" );
// args
$condition1 = array(
'posts_per_page' => 1,
'post_type' => 'cta',
'post-status' => 'publish',
'orderby' => 'rand',
'meta_query' =>array(
'relation' => 'AND',
array(
'key' => 'launch',
'value' => $today,
'compare' => '<'
),
array(
'key' => 'expiry',
'value' => $today,
'compare' => '>'
),
)
);
$condition2 = array(
'posts_per_page' => 1,
'post_type' => 'cta',
'post-status' => 'publish',
'orderby' => 'rand',
'meta_query' =>array(
array(
'key' => 'launch',
'value' => $today,
'compare' => '<'
),
)
);
// query
$cta1 = new WP_Query( $condition1 );
$cta2 = new WP_Query( $condition2 );
if( !empty($cta1 -> have_posts()) ) :
while( $cta1 -> have_posts() ) : $cta1 -> the_post(); global $post;
// Display post with first condition
endwhile; wp_reset_postdata();
elseif( ( $cta2 -> have_posts() ) ) :
while( $cta2 -> have_posts() ) : $cta2 -> the_post(); global $post;
// Display post with second condition
endwhile; wp_reset_postdata();
endif;

You can use the advanced meta query option
$meta_query = array(
'posts_per_page' => 1,
'post_type' => 'cta',
'post-status' => 'publish',
'orderby' => 'rand',
'meta_query' =>array(
'relation' => 'OR',
array(
'key' => 'launch',
'value' => $today,
'compare' => '<'
),
array(
'relation' => 'AND',
array(
'key' => 'launch',
'value' => $today,
'compare' => '<'
),
array(
'key' => 'expiry',
'value' => $today,
'compare' => '>'
),
),
)
);

Related

Why this will not output some results?

$args = array(
'post_type' => 'cp_test',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'team',
'value' => 'oud',
'compare' => '='
),
array(
'key' => 'team',
'value' => 'jeugd',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while ( $the_query->have_posts() ) : $the_query->the_post();
$rows = get_field('wedstrijd');
if($rows) :
foreach($rows as $row):
echo $row['stand'];
endforeach;
endif;
endwhile;
endif;
This wil not return any output why? Without the meta_query it wil post all data but not with the meta_query. Please help me!
The args seems to be wellformed, but you can check the construction of the arguments here https://www.billerickson.net/code/wp_query-arguments/ to see if you are doing it well. If this is correct I would check if the metakey are written correct, and try with only one meta_query to debug that error.
If you want the value of key 'relation' be an array, ypou should use this this:
$args = array(
'post_type' => 'cp_test',
'meta_query' => array(
'relation' => array(
'OR',
array(
'key' => 'team',
'value' => 'oud',
'compare' => '='
),
array(
'key' => 'team',
'value' => 'jeugd',
'compare' => '='
))
)
);

WP query and multiple key

I m trying to implement a multiple meta key filter in my wordpress. That's simple I get value to a form to filter my post. If I implement this with only "price" query wordked perfectly. If I add "genre" nothing work, query not working.
For field "genre" I m using checkbox from Advanced Custom Fields with this structure "homme : Homme / femme : Femme".
I test different thing like delete "price" and query on "genre" not working too...
I get value from this
<?php
if($_GET['minprice'] && !empty($_GET['minprice']))
{
$minprice = $_GET['minprice'];
} else {
$minprice = 0;
}
if($_GET['maxprice'] && !empty($_GET['maxprice']))
{
$maxprice = $_GET['maxprice'];
} else {
$maxprice = 1000;
}
if($_GET['genre'] && !empty($_GET['genre']))
{
$genre = $_GET['genre'];
}
?>
my query looks like this
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
)
)
);
My loop with my query
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
)
)
);
// create a new instance of WP_Query
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<?php
get_template_part( 'content-category', get_post_format() );
?>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<div class="clearfix"></div>
<?php bootstrap_pagination();?>
<?php } ?>
<?php else: ?>
<?php get_template_part( 'no-results', 'archive' ); ?>
<?php endif; ?>
</div>
<?php wp_reset_query(); ?>
I tested this and it's work !
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
)
);
But that don't work
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
)
)
);
Please, can you help me beacause I m loosing my mind....
Thanks !
I think you are missing an wrapping array in the meta_query
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
array(
'relation' => 'AND',
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array( $minprice, $maxprice ),
'compare' => 'BETWEEN'
),
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
),
),
),
);

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 );

How do I combine these two Wordpress search queries?

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 );

Query Wordpress Posts for multiple meta keys with multiple values

Im having trouble with my WordPress query on a project I do for a customer of us. Basically my problem is that I want to grab posts from the database with specified meta_keys. I want to get my posts (wpcompare) with the meta key '_price' and value between xx and yy. Works fine so far. Now I want to add filters for manufacturer, categories and tags. These filter values are all multiple, so that you can select multiple manufacturers. For example Canon and Nikon. So with the WP_MetaQuery I can filter multiple meta_keys and values, but I cant combine the queries with AND or OR.
Let me give you an example, how my query should work:
"Give me all the posts with post_type "wpcompare" where the meta_value _price is between 100 and 200 bucks, and where the manufacturer (_hersteller) is Canon OR Nikon OR Sony".
My head turns crazy, so please help me.
Thank you very much in advance :-)
Here is my Code:
if(isset($_POST) AND !empty($_POST))
{
$meta_query = array(
'relation' => 'AND',
array(
'key' => '_price',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
);
}
else
{
$meta_query = '';
}
$args = array(
'post_type' => 'wpcompare',
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => $meta_query,
'posts_per_page' => ($per_page == false) ? 18 : $per_page,
'ignore_sticky_posts'=> true
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args)
$args = array(
'post_type' => 'posttypehere',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_price',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'somekey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'anotherkey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ($query->have_posts()) : $query->the_post();
echo $post_id = get_the_ID();
endwhile;
endif;
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_price',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'somekey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'anotherkey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
);

Categories