On taxonomy page, show posts associated with that taxonomy and another - php

I have a custom post type called Developments. They are buildings. They have two different taxonomies: City and Status. On the City taxonomy page for one of the buildings I'm trying to show all posts associated with the Status taxonomy.
<?php
// Vars
$status_taxonomy = 'development_status';
$devs = get_terms( array(
'taxonomy' => $status_taxonomy,
'hide_empty' => true,
) );
?>
<?php
foreach($devs as $dev) :
$dev_id = $dev->term_id;
$dev_name = $dev->name;
?>
<?php
$term_slug = $dev->slug;
$dev_posts = new WP_Query( array(
'post_type' => 'developments',
'posts_per_page' => -1, //important for a PHP memory limit warning
'tax_query' => array(
array(
'taxonomy' => $status_taxonomy,
'field' => 'slug',
'terms' => $term_slug,
'operator' => 'IN',
),
),
));
if( $dev_posts->have_posts() ) :
echo '<h3>'. $dev_name .'</h3>';
while ( $dev_posts->have_posts() ) : $dev_posts->the_post();
?>
<div class="col-xs-12">
<h3>$dev_name</h3>
</div>
<div class="col-md-4">
<h4><?php the_title(); ?></h4>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<?php
endforeach;
?>
This code outputs all of the Status terms, but it's showing all the posts too, I need it to show posts that are associated to City. I am not sure how to modify the code above to achieve this.

Get your current taxonomy and term on the City taxonomy page like this:
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
And then change you tax_query to add another array so that the current city taxonomy is added to the restrictions:
'tax_query' => array
(
'relation' => 'AND',
array(
'taxonomy' => $status_taxonomy,
'field' => 'slug',
'terms' => $term_slug,
'operator' => 'IN',
),
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $current_term->slug,
'operator' => 'IN',
),
),

Related

WordPress: get all posts included in two taxonomies (WP_Query tax_query relation AND)

Of course, this can be counted as a duplicate, but I can't beat this task in any way, I'm just learning)
There are several taxonomies, for example, location and set. I need to output the number of posts included in both taxonomies. My function now looks like this:
function get_all_property_count($id, $setid)
{
$count = new WP_Query(array(
'nopaging' => true,
'post_type' => 'property',
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'location',
'field' => 'id',
'fields' => 'ids',
'terms' => $id,
),
array(
'relation' => 'AND',
array(
'taxonomy' => 'set',
'field' => 'id',
'fields' => 'ids',
'terms' => $setid,
'include_children' => true,
)
)
),
));
return $count->post_count;
}
Then I use it to get a counter of posts published in the current taxonomy and another by id (I'm not sure if I'm explaining it correctly().
<div class="count_property">
<div class="sell">
<span class="strong">Sell:</span> <?php echo get_all_property_count($term->term_id, 30); ?>
</div>
<div class="rent">
<span class="strong">Rent:</span> <?php echo get_all_property_count($term->term_id, 31); ?>
</div>
</div>
Where $term->term_id, category id from location taxonomy. 30 and 31 sale and rental categories from set taxonomy.
At the exit I get one more in sell. For example,Sell: 2 (although there should be 0 in general), Rent: 1 (that's right).
Try the following solution
function get_post_count_by_term_slug($post_type,$taxonomy,$term_slug) {
$current_obj = get_queried_object();
// Check if we are on specific taxonomy page like location
if(!is_tax('location')) return;
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => $current_obj->taxonomy, // Here we get current location
'field' => 'id',
'terms' => $current_obj->term_id
),
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term_slug
)
),
);
$count = new WP_Query($args);
if($count->have_posts()):
echo $count->post_count;
else:
echo 'Nothing found';
endif;
}
// Use it like this get_post_count_by_term_slug('post_type','taxonomy_name','tax_term_slug');
<div class="count_property">
<div class="sell">
<span class="strong">Sell:</span> <?php get_post_count_by_term_slug('property','set','sell'); ?>
</div>
<div class="rent">
<span class="strong">Rent:</span> <?php get_post_count_by_term_slug('property','set','rent'); ?>
</div>
</div>

Woocommerce Tax Query that finds the next available product (closest attribute)

i'm using Ajax and the following function (in functions.php) to pull in a product that has matching attributes. All works well but if there are no products that have an exact attribute (pa_three) then i want it to fetch a product with the next closest pa_three. pa_three is a length.
Any ideas on how i can achieve this?
<?php
function example_filter_function(){
$final_one = $_POST['final_one'];
$final_two = $_POST['final_two'];
$final_three = $_POST['final_three'];
$args = array(
'orderby' => 'date',
'post_type' => 'product',
'posts_per_page' => 1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'terms' => 1,
),
array(
'taxonomy' => 'pa_one',
'field' => 'id',
'terms' => $final_one
),
array(
'taxonomy' => 'pa_two',
'field' => 'id',
'terms' => $final_two
),
array(
'taxonomy' => 'pa_three',
'field' => 'slug',
'terms' => $final_three
)
)
);
$query = new WP_Query($args); if ($query->have_posts()) : while ($query->have_posts()): $query->the_post();?>
<h3><?php the_title();?></h3>
<?php endwhile; wp_reset_postdata();
die();
}
add_action('wp_ajax_myfilter', 'example_filter_function');
add_action('wp_ajax_nopriv_myfilter', 'example_filter_function');
?>

WP_Query cpt taxonomy not loading posts

I have created some taxonomies and post_types trough custom post type UI plugin.
I pass taxonomy_id with a form to a page and I receive it correctly [var_dump($_POST)] shows me number example 30. I want to show posts from that custom post type category: I tried the code below but it returns nothing.
$args = [
'tax_query' => array(
array(
'taxonomy' => 'school_type',
'field' => 'term_id',
'terms' => array('30','22'),
// 'operator' => 'IN'
),
'post_type' => 'school',
)
];
if($q->have_posts()):
while($q->have_posts()){
$q->the_post();
echo the_title();
}
else:
echo 'nothing';
endif;
Can anyone help me?
You should have a $arg variable like this and then use the WP_Query() object to get your posts.
$args = array(
'post_type' => 'school',
'posts_per_page'=>30,
'post_status' => 'publish',
'tax_query' => array(array(
'taxonomy' => 'school_type',
'field' => 'term_id',
'terms' => array('30','22'),
);
$q = new WP_Query($args);
if($q->have_posts()):
while($q->have_posts()){
$q->the_post();
echo the_title();
}
else:
echo 'nothing';
endif;
Put $q = new WP_Query( $args ); before your if line
change your permalink to post type and update permalinks, hope this will help.
You have the post_type in the tax_query array, plus you're targeting $q when it doesn't exist (not that I can see. anyway).
Try something like this :-
$args = array(
'post_type' => 'school',
'tax_query' => array(
array(
'taxonomy' => 'school_type',
'field' => 'term_id',
'terms' => array('30','22'),
),
),
'numberposts' => -1
);
$q = new WP_Query($args);
if($q->have_posts()):
while($q->have_posts()){
$q->the_post();
echo the_title();
}
else:
echo 'nothing';
endif;

how to list custom post type 'movies' with a genre of 'horror'

I have been trying to list my custom type 'movies' with the custom taxonomy genre with a value of drama.
I have tried several solutions but as yet I have failed.
<?php
$args = array(
'post_type' => 'movies',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'term_id',
'terms' => 'drama'
)
)
);
$drama = new WP_Query( $args );
if ( $drama->have_posts() ) : while ( $drama->have_posts() ) : $drama->the_post(); ?>
<h4><?php echo the_title(); ?></h4>
<?php endwhile; ?>
<?php else : echo '<p>NO CONTENT FOUND</p>'; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Hope someone can shed some light on this matter.
Steven
The field parameter is wrong, in your case you need to set it to slug as drama is not a term_id
$args = array(
'post_type' => 'movies',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'drama'
)
)
);
Hope it works !

How to show post for a particular term of custom taxonomy?

I have created a custom taxonomy for a custom post. Now i want to show all post related to a particular term. Suppose i have two terms term1 and term2. When click on term1 then all post related to term1 will show and post , that is related to term2 will not show. But now when i click term1 then post related to term1 and term2 are showing at a time. I have wrote the following code to taxonomy.php template.
<div class="main-content">
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$the_query = new WP_Query( array(
'post_type' => array('newsbox_post'),
'tax_query' => array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->name,
),
) );
?>
<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
<?php echo get_the_term_list( $post->ID, $term->taxonomy, 'People: ', ', ' ); ?>
<hr />
</div>
<?php
endwhile;
wp_reset_postdata();
else:?>
<h3><?php _e('404 Error: Not Found'); ?></h3>
<?php
endif;
?>
</div>
Please tell me , how can i achieve that.
I have got the answer .
$the_query = new WP_Query( array(
'post_type' => array('newsbox_post'),
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
),
) );
You should use slug instead of name like following code:-
$the_query = new WP_Query( array(
'post_type' => array('newsbox_post'),
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
),
) );
Hope this will help you.

Categories