tax_query with custom taxonomy not working as expected - wordpress

I'm hoping someone can help spot what the problem is. I have a custom taxonomy called "events_cat" I am trying to get all the post types "event" that are in the taxonomy term 11 to display, however the below code is pulling in events that are not in that taxonomy term and I can't see the mistake. Any ideas what might be causing the issue?:
<?php
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'events_cat',
'field' => 'term_id',
'terms' => array(11),
),
));
$upcomingEvents = new WP_Query($args); ?>

$custom_args=array(
'post_type' => "event",
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> -1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'tax_query' => array(
array(
'taxonomy' => 'events_cat',
'field' => 'id',
'terms' =>"11"
)
),
'orderby' => 'id',
'order' => 'ASC'
);
$custom_my_query = null;
$custom_my_query = new WP_Query($custom_args);
$custom_my_total_count = count($custom_my_query);
if( $custom_my_query->have_posts() )
{
while ($custom_my_query->have_posts()) : $custom_my_query->the_post();
?>
<?php echo get_the_title($post->ID);?>
<?php
endwhile;
}
wp_reset_query($custom_my_query); // Restore global post data stomped by the_post().
}

Related

Wordpress filter by taxonomy and expired acf date field

I have a custom post type "events" with a custom taxonomy "events_cat" which has category-1, category-2 etc.
This is my code in taxonomy.php
<?php
$today = date('Ymd');
$cat_slug = get_queried_object()->slug;
$args = array(
'post_type' => 'events',
'nopaging' => true,
'meta_key' => 'expiry',
'tax_query' => array(
array(
'taxonomy' => 'events_cat',
'field' => 'slug',
'terms' => $cat_slug
)
),
'meta_query' => array(
array(
'key' => 'expiry',
'value' => $today,
'compare' => '>='
)
)
);
$events = new WP_Query( $args );
if ( $events->have_posts() ) :
?>
<?php while ( $events->have_posts() ) : $events->the_post(); ?>
// my code
<?php endwhile; wp_reset_postdata(); ?>
<?php endif; ?>
What I need is to filter my post by category and if it is expired or not at the same time.
I explain better: I have one page with all expired posts and another one with all not expired posts.
What I need is that if I click on a category in the "expired page" I just want the list of the expired posts and vice versa.
I was thinking of a condition to change the compare value depending on whether or not the post has expired but I can't figure out how I can do it.
EDIT
I tried such a thing but of course it doesn't work because $expiry is null.
if($expiry >= $today) {
$compare = '>=';
} else {
$compare = '<';
}
$args = array(
'post_type' => 'events',
'nopaging' => true,
'meta_key' => 'expiry',
'tax_query' => array(
array(
'taxonomy' => 'events_cat',
'field' => 'slug',
'terms' => $cat_slug
)
),
'meta_query' => array(
array(
'key' => 'expiry',
'value' => $today,
'compare' => $compare
)
)
);
Does anyone have a better idea on how this can be done or point me in the right direction?
I finally solved the problem by using the $_GET super global variable. I don't know if it's the most correct method but it's the only one I've found. If anyone has a better idea please let me know.
I added "?expired" to the category links of expired posts so I can do this:
$today = date('Ymd');
$compare = '>=';
if( isset($_GET['expired'])) {
$compare = '<';
}
args = array(
'post_type' => 'events',
'nopaging' => true,
'meta_key' => 'expiry',
'tax_query' => array(
array(
'taxonomy' => 'events_cat',
'field' => 'slug',
'terms' => $cat_slug
)
),
'meta_query' => array(
array(
'key' => 'expiry',
'value' => $today,
'compare' => $compare
)
)
);

Wordpress: Multiple taxonomy terms in WP_Query

I am trying to get multiple taxonomy terms in WP_Query. Here is my code:
$category = get_field('portfolio_category'); //array of IDs like 14, 15, 16
<?php
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => array( implode(', ', $category ) ),
'operator' => 'AND'
)
),
));
?>
The problem is in the current code I can only query the very first term.
For example I can only get projects from cat ID=14.
What am I doing wrong here? How can I query posts from multiple terms?
Thank you.
Since $category is already an array... change your terms value to just $category. so your full query would be:
$the_query = new WP_Query(array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'projectCategories',
'field' => 'term_id',
'terms' => $category,
'operator' => 'AND'
)
),
));

how to set order of custom post type term and post's

hello guys i am using custom post type for showing plans. I want to customise the order of term and it's post for that i am using below code
<?php
$plan_group = get_terms('numbers_plans', array(
'orderby' => 'description', //this is for term order it's working
'order' => 'ASC'
));
foreach ( $plan_group as $plan_group_term ) {
$plan_group_query = new WP_Query( array(
'post_type' => 'numbers_plan',
'tax_query' => array(
array(
'taxonomy' => 'numbers_plans',
'field' => 'slug',
'terms' => array( $plan_group_term->slug )
)
)
));
?>
anyone please tell me how can i set order of my post's.
Hey Guys i had done it below code is working for me!!
<?php
$plan_group = get_terms('numbers_plans', array(
'orderby' => 'description',
'order' => 'ASC'
));
foreach ( $plan_group as $plan_group_term ) {
$plan_group_query = new WP_Query( array(
'post_type' => 'numbers_plan',
'meta_key' => 'plan_minutes',
'orderby' => '0',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'numbers_plans',
'field' => 'slug',
'terms' => array( $plan_group_term->slug )
)
)
));
?>

Filter tax_query in my wordpress

I would like to know how i filters property area , i Try :
<?php
$args = array(
'post_type' => 'estate_property',
'post_status' => 'publish',
'tax_query' => array(
'taxonomy' => 'property_area',
'field' => 'slug',
'terms' => 'pigalle',
),
);
$selection = new WP_Query($args);
?>
But all show ! WHY ? lol
Thanks
Read up on Taxonomy Parameters
The tax_query parameter is a multi-dimensional array. You need to wrap it with another array:
<?php
$args = array(
'post_type' => 'estate_property',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'property_area',
'field' => 'slug',
'terms' => 'pigalle'
)
)
);
$selection = new WP_Query($args);
?>

WP_query twice on a single taxonomy

I have a simple WP_query on a custom post and works fine:
$args = array(
'post_type' => 'post-type',
'showposts' => 2,
'location' => 'london'
);
but I want it to query two locations so used an array:
$args = array(
'post_type' => 'post-type',
'showposts' => 2,
'location' => array('london','paris')
);
but it only pulls in the last term. Is there another way that I should be doing this?
$args = array(
'post_type' => 'post-type',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => array('london','paris')
)
)
);
Try this

Resources