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'
),
),
),
);
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 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 didn't know that get_posts doesn't work with loop.php. I already have this great loop.php file and I want to use it.
I created this code:
$pageposts = get_posts(
array(
'relation' => 'AND',
'post__in' => $postid,
'post_type' => 'event',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'st_date',
'value' => array($todate_s, $frmdate_s),
'compare'=> 'BETWEEN',
'type' => 'DATE'
),
),
)
);
if ( have_posts() ) : ?>
<?php get_template_part('loop'); ?>
<?php else : ?>//etc
How I can convert the get_posts(//etc) to something that work with have_posts() and get_template_part('loop') at the end of my code?
Is this possible?
You get confused between query_posts(); and get_posts(); try this:
<?php
$pageposts = get_posts(
array(
'relation' => 'AND',
'post__in' => $postid,
'post_type' => 'event',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'st_date',
'value' => array($todate_s, $frmdate_s),
'compare'=> 'BETWEEN',
'type' => 'DATE'
),
),
)
);
foreach ( $pageposts as $post ) : setup_postdata( $post );
get_template_part('loop');
endforeach;
wp_reset_postdata();
?>
http://codex.wordpress.org/Template_Tags/get_posts
<?php
query_posts(
array(
'relation' => 'AND',
'post__in' => $postid,
'post_type' => 'event',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'st_date',
'value' => array($todate_s, $frmdate_s),
'compare'=> 'BETWEEN',
'type' => 'DATE'
),
),
)
);
get_template_part('loop');
wp_reset_query();
?>
http://codex.wordpress.org/Function_Reference/query_posts
I am using WPML and ACF in my WP.
Now I wanna list posts from the category ID 399 with the ACF Field "organization_type" and the value key "socialbusiness" but they do not show up.
This are my query tries:
$args = array(
'post_type' => 'post',
'cat' => 399,
'posts_per_page' => -1,
'meta_query' => array(
//'relation' => 'OR',
array(
'key' => 'organization_type',
'value' => 'socialbusiness',
//'compare' => '='
)
)
);
//unset($args);
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'cat' => 399,
'meta_key' => 'organization_type',
'meta_value' => 'socialbusiness'
);
// query
query_posts( $args );
while( have_posts() ) {
What am I doing wrong?
You should have just one variable $args because your first declaration of the variable is override by your second variable.
In your case your code should look like :
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => 399
)
),
'meta_query' => array(
array(
'key' => 'organization_type',
'value' => 'socialbusiness',
'compare' => '=',
'type' => 'CHAR'
),
)
);
$items = new WP_Query($args);
?>
<?php if($items->have_posts()) : ?>
<div class='item'>
<?php while($items->have_posts()) : $items->the_post() ?>
.....
<?php endwhile ?>
</div>
<?php endif ?>
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
)
);