$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' => '='
))
)
);
Related
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' => '>'
),
),
)
);
I'm trying to list an archive of queried custom post types named 'jobs', each with a custom field of 'minimum_salary' and 'maximum_salary'.
On the previous search page the user enters two values in the search ('min-salary' & 'max-salary'), which are added to the URL of the archive page.
I want to be able to display ALL jobs that have a 'minimum_salary' AND 'maximum_salary' BETWEEN the min and max salary var the user entered on the previous page.
See below for my code so far, thanks for the help.
<?php
$minSalaryVar = get_query_var('min-salary');
$maxSalaryVar = get_query_var('max-salary');
$taxArgs = array();
if ($minSalaryVar && $maxSalaryVar) {
$taxArgs[] = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'minimum_salary',
'value' => array($minSalaryVar, $maxSalaryVar),
'compare' => 'BETWEEN',
'type' => 'numeric'
),
array(
'key' => 'maximum_salary',
'value' => array($minSalaryVar, $maxSalaryVar),
'compare' => 'BETWEEN',
'type' => 'numeric'
),
)
);
}
if ($taxArgs) {
$args = array (
'tax_query' => $taxArgs,
);
$argsNew = array_merge( $wp_query->query_vars, $args );
query_posts( $argsNew );
} ?>
You are using 'compare' = 'BETWEEN' two times when you need either using it just once or compare twice one greater than minimum salary and the other bigger. Following I'll let both options:
1:
if ($minSalaryVar && $maxSalaryVar) {
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'minimum_salary',
'value' => $minSalaryVar,
'compare' => '>=',
'type' => 'numeric'
),
array(
'key' => 'maximum_salary',
'value' => $maxSalaryVar,
'compare' => '<=',
'type' => 'numeric'
),
);
}
if ($taxArgs) {
$args = array (
'meta_query' => $meta_query,
);
$argsNew = array_merge( $wp_query->query_vars, $args );
query_posts( $argsNew );
} ?>
2:
$taxArgs = array();
if ($minSalaryVar && $maxSalaryVar) {
$meta_query = array(
'key' => 'minimum_salary',
'value' => array($minSalaryVar,$maxSalaryVar),
'compare' => 'BETWEEN
'type' => 'numeric'
);
}
if ($taxArgs) {
$args = array (
'meta_query' => $meta_query,
);
$argsNew = array_merge( $wp_query->query_vars, $args );
query_posts( $argsNew );
} ?>
--- EDIT
You should use 'meta_query' instead of 'tax_query'
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'
),
),
),
);
I am looking for a code that can contain all queries in one.
The part of the code is :
<?php $query = new WP_Query(array ( 'post_type' => 'post', 'posts_per_page' => '6', 'order' => 'DESC', 'tax_query' => array(
array(
'taxonomy' => 'type-article',
'field' => 'slug',
'terms' => array( 'interview', 'tribune' )
) ) ));
while ( $query->have_posts() ) :
$query->the_post(); ?>
<?php if($post->post_type == "post"){ $version_FR = get_field('versionFRexiste'); $langue = get_field('langue'); }; ?>
<?php if($langue == "FR" || ($langue == "EN" && $version_FR == "Non")) : ?>
[some code]
<?php endif; ?>
<?php endwhile; ?>
inside the WHILE, you find two lines with IF
I would like to include these condition in the query at the TOP where WP_Query.
This code is working but my problem is that I would like to have the same number of results to display (6 here)
Thanks for your help
Please have a look here on the ACF Docs.
The answer is to write a meta query within the WP-Query, such as:
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'color',
'value' => array('red', 'orange'),
'compare' => 'IN',
),
array(
'key' => 'featured',
'value' => '1',
'compare' => '=',
),
),
));
Thanks. It was not so easy... but the metaquery won
<?php $query = new WP_Query(array ( 'post_type' => 'post', 'posts_per_page' => '5', 'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'langue',
'value' => 'FR'
),
array(
'relation' => 'AND',
array(
'key' => 'langue',
'value' => 'EN'
),
array(
'key' => 'versionFRexiste',
'value' => 'Non'
)
)
),
'tax_query' => array(
array(
'taxonomy' => 'type-article',
'field' => 'slug',
'terms' => array( 'interview', 'tribune' )
) ) ));
Hope this will help those who need an answer
I have a search form where my form action is this:
<form action="<?php echo esc_url(home_url('/searched-result')); ?>" method="post">
On the searched-result template I have this query,
<?php
// Only check these form fields (change the list as needed...)
$fields = array( 'custom_price', 'custom_beds', 'custom_garage');
foreach( $fields as $field ) {
if( $_REQUEST[$field] != '' ) {
// We have something to match, otherwise ignore the field...
$meta_query[] = array(
'key' => $field,
'value' => $_REQUEST[$field], // This is OK, WP_Query will sanitize input!
'compare' => 'LIKE'
);
}
}
$args = array(
'post_type' => 'property_post',
'posts_per_page' => 1000,
'meta_key' => 'SORTFIELD', // The name of the metakey to orderby
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => $meta_query,
);
$query = new WP_Query( $args );
if (have_posts()) : while (have_posts()) : the_post();
?>
<h1 class="title">
<?php the_title(); ?></h1>
The way you have it will only return results if all meta_values match the criteria. You must add an extra argument called relation, and set it as 'OR': Example:
$args = array(
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query = new WP_Query( $args );
Not tested :
$fields = array( 'custom_price', 'custom_beds', 'custom_garage');
foreach( $fields as $field ) {
if( isset($_REQUEST[$field]) && !empty($_REQUEST[$field]) ) {
// We have something to match, otherwise ignore the field...
$meta_query[] = array(
'key' => $field,
'value' => $_REQUEST[$field], // This is OK, WP_Query will sanitize input!
'compare' => 'LIKE'
);
}
}
Check if your getting value in $_REQUEST.
Hope this helps...
$args = array(
'post_type' => 'property_post',
'posts_per_page' => 1000,
'meta_key' => 'SORTFIELD', // The name of the metakey to orderby
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
$meta_query
)
);