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
Related
This is the code I am using:
$results = array(
'post_type' => 'score',
'tax_query' => array(
array(
'taxonomy' => 'competitions',
'field' => 'slug',
'terms' => $tax->slug,
'compare' => '='
)
),
'meta_query' => array(
array(
'numberposts' => -1,
'post_type' => 'score',
'meta_key' => 'horse_name',
'meta_value' => $horsename,
'compare' => 'LIKE'
)
)
);
// query
$the_query = new WP_Query($results);
The problem is I can't work out how to make a Boolean AND expression so that both the taxonomy and the custom field should match before returning a post.
I revised your code. check below code.
$results = array(
'post_type' => 'score',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'competitions',
'field' => 'slug',
'terms' => $tax->slug,
'compare' => '='
)
),
'meta_query' => array(
array(
'meta_key' => 'horse_name',
'meta_value' => $horsename,
'compare' => 'LIKE'
)
)
);
// query
$the_query = new WP_Query( $results );
I have a custom post type cota where all data is displayed in a <table> in my site. I need to order this table by:
administradora - some kind of "category" that the user specifies. Ordered ASC (a-z);
valor - The price of the cota. Each cota has a price. Ordered DESC (9 - 0) ;
The problem : It's already ordered by administradora, but not correctly ordered by valor.
Example: The order by valor displays first a cota with US$9.000,00 and then another cota with US$1.000.000,00. It's wrong. The one million dollar cota should come first, and then the nine thousand dollars cota.
Here's an image that shows better the situation
Code for ordering the cota:
$query = new WP_Query( array(
'post_type' => 'cota',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tipo',
'field' => 'slug',
'terms' => $atts['tipo'],
),
),
'orderby' => 'meta_value_num',
'meta_query' => array(
'relation' => 'AND',
'valor_clause' => array(
'key' => 'valor',
'compare' => 'EXISTS'
),
'adm_clause' => array(
'key' => 'administradora',
'compare' => 'EXISTS'
)
),
'orderby' => array(
'adm_clause' => 'ASC',
'valor_clause' => 'DESC'
)
)
);
EDIT 1: After some research, i found out that wordpress saves all its data as longtext in the database. But still cannot solve the problem.
I can provide more info if needed.
Try Below Code:
$query = new WP_Query( array(
'post_type' => 'cota',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tipo',
'field' => 'id',
'terms' => $atts['tipo'],
),
),
'orderby' => 'meta_value_num',
'meta_query' => array(
'relation' => 'AND',
'valor_clause' => array(
'key' => 'valor',
'compare' => 'EXISTS'
),
'adm_clause' => array(
'key' => 'administradora',
'compare' => 'EXISTS'
)
),
'orderby' => array(
'adm_clause' => 'ASC',
'valor_clause' => 'DESC'
)
)
);
SOLVED
The following code should work:
$query = new WP_Query( array(
'post_type' => 'cota',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tipo',
'field' => 'slug',
'terms' => $atts['tipo'],
),
),
'meta_query' => array(
'relation' => 'AND',
'credito_clause' => array(
'key' => "valor",
'orderby' => 'meta_value_num',
'type' => 'DECIMAL',
'compare' => 'EXISTS'
),
'adm_clause' => array(
'key' => 'administradora',
'compare' => 'EXISTS'
)
),
'orderby' => array(
'adm_clause' => 'ASC',
'credito_clause' => 'DESC'
)
)
);
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 want to retrieve result from two different custom post type.I queried for tax_query , meta_query and another meta_query but retriving no result.there should be one result.
Below is the codes,
$args = array(
'post_type' => 'listings',
's' => get_query_var( 's' ),
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
),
'tax_query' => array(
'relation' => 'OR',
),
'posts_per_page' => 15,
);
if(isset($_GET['city']) && !empty($_GET['city'])){
$args['tax_query'][] = array(
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $selected_city,
'relation' => 'OR',
),
);
}
if(isset($_GET['cuisine'])){
$args['tax_query'][] = array(
array(
'taxonomy' => 'cuisine',
'field' => 'slug',
'terms' => $selected_cuisine,
'relation' => 'OR',
),
);
}
if(isset($_GET['st'])){
$args['meta_query'][] = array(
array(
'key' => 'restaurent_location',
'value' => $location,
'compare' => 'LIKE',
),
);
}
if(isset($_GET['has_takeaway'])){
$args['meta_query'][] = array(
array(
'key' => 'has_resturent_takeaway',
'value' => $takeaway,
'compare' => '=',
),
);
}
$arg2 = array(
'post_type' => 'reviews',
'meta_query' => array(
'relation' => 'OR',
),
);
if(isset($_GET['rating_range'])){
$args2['meta_query'][] = array(
array(
'key' => 'review_rating',
'value' => array( $rating_range[0], $rating_range[1] ),
'compare' => 'BETWEEN',
),
);
}
$query = new WP_Query( array_merge($args, $args2) );
get_query_var( 's' ) should be just $s after you've set $s. Right now it's not searching for anything.
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'
),
);