WordPress Advanced Custom Fields - Meta Query no result - php

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 ?>

Related

WP Query filter by ACF field don't work

I'm having an issue with WP query filtered by ACF Post Object Field.
I have to query the 'post' filtered by 'author' acf field.
i'm using this code but this don't work
$post_type_query = new WP_Query(
array (
'post_type' => 'post',
'posts_per_page' => 3,
'meta_query' => array(
array(
'key' => 'author',
'value' => 'prova'
)
)
)
);
Thereis one article on wordpress post with 'prova' author, but the query return empty.
I can't understand why
Thanks
Try this:
$postData = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 3,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'author',
'value' => 'prova',
'compare' => '=' // or if you want like then use 'compare' => 'LIKE'
)
)
)
);
if($postData->have_posts()):
while ($postData->have_posts()): $postData->the_post();
echo "Post Title";
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
endif;

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

wordpress - category__not_in not working

I'm having a problem getting my query function. I need to run the loop, excluding a particular category.
I'm trying to use category__not_in, but is not working at all some.
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__not_in' => array( '44' ),
'posts_per_page' => 9,
'paged' => get_query_var('paged')
);
$query = new WP_Query( $args );
query_posts($query);
?>
I've already tried:
'category__not_in' => array( '44' ),
'category__not_in' => array( 44 ),
'category__not_in' => '44',
'category__not_in' => 44,
But nothing works =(
Try using tax_query instead :
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => get_query_var('paged'),
'tax_query' => array(
array(
'taxonomy' => '<YOUR TAXONOMY NAME>',
'field' => 'term_id',
'terms' => array( 44 ),
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
query_posts($query);
?>
Use 'cat' => '-44' in your $args array:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => '-44',
'posts_per_page' => 9,
'paged' => get_query_var('paged')
);
It's the way recommended in the WP Codex.
Thanks guys, it worked thanks to #rnevius
The problem was in my query, I was using WP_Query() and query_posts().
I used how reference the WP Codex: https://codex.wordpress.org/Class_Reference/WP_Query
Below is how my code was at the end:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__not_in' => array( 44 ),
'posts_per_page' => 9,
'paged' => get_query_var('paged')
);
$query = new WP_Query( $args );
?>
<?php
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
// code
<?php
}
} else {
// no posts found
}
wp_reset_postdata();
?>
To exclude a category in the search use this:
function search_filter($query)
{
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search)
{
$taxquery = array(
array(
'taxonomy' => 'category',
'field' => 'term_taxonomy_id',
'terms' => 244,
'operator' => 'NOT IN',
)
);
$query->set( 'tax_query', $taxquery );
}
}
}
add_action('pre_get_posts','search_filter');

Wordpress Query + ACF Meta Query

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

Change get_posts to loop.php suitable version

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

Categories