exclude a custom taxonomy from wp_query - php

IN my wp site, I have 2 category and a few post like that..
cat_1- post 1, post 2, post 3.
cat_2- post 2, post 3, post 4.
When i am in post 3 page, i want to show releted article only from category 2.here is my code:but it returns empty. Probably i did not catch the logic. Any help would be highly appreciated.
<?php
$terms = get_the_terms( $post_id, 'category' );
if( empty( $terms ) ) $terms = array();
$term_list = wp_list_pluck( $terms, 'slug' );
$related_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'post__not_in' => array( get_the_ID() ),
'orderby' => 'desc',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term_list
),
array(
'taxonomy' => 'category',
'terms' => array('cat_1'),
'field' => 'slug',
'operator' => 'NOT IN',
),
),
);
$related = new WP_Query( $related_args );
if( $related->have_posts() ):
?>
<div class="post-navigation">
<h3>Related posts</h3>
<ul>
<?php while( $related->have_posts() ): $related->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
</div>
<?php
endif;
wp_reset_postdata();
?>

here is the following query ...may be helpful to you...
$custom_tex=array(array('taxonomy'=>'category','field'=>'slug','terms'=>'cat_2'));
$new = new WP_Query(array('post_type'=>'post','posts_per_page'=>-1, 'tax_query'=>$custom_tex,'order' => 'DESC','orderby' => 'ID' ));
while ($new->have_posts()) : $new->the_post();
echo $post->post_title;
echo "<br>";
endwhile;

Related

How do I filter these results by a custom taxonomy?

I've got a custom post type that I've sorted by date from an ACF date picker field. I need to be able to filter these results based on a custom taxonomy that I've got. I can't seem to work out where to add the tax_query array. Keeps breaking the site.
Taxonomy name is 'pre_job_status', term to filter by is 'survey-complete'
Currently I have the following code
<?php
// get posts
$posts = get_posts(
'tax_query' => array(
array(
'taxonomy' => 'pre_job_status',
'field' => 'slug',
'terms' => array( 'survey-complete' )
),
),
array(
'post_type' => 'pre_jobs',
'posts_per_page' => -1,
'meta_key' => 'survey_date',
'orderby' => 'meta_value',
'order' => 'ASC',
));
if( $posts ): ?>
<hr>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<?php $requestor = get_field('pre_job_requestor', $client->ID ); ?>
<?php $survey_site = get_field('survey_site', $client->ID ); ?>
<li>
<?php the_field('survey_date'); ?> - <?php the_title(); ?> - <?php echo $requestor[0]->post_title; ?> - <?php echo $survey_site[0]->post_title; ?>
</li><hr>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Your get_posts() query should look like the following:
// get posts
$posts = get_posts(array(
'post_type' => 'pre_jobs',
'posts_per_page' => -1,
'meta_key' => 'survey_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'pre_job_status',
'field' => 'slug',
'terms' => array( 'survey-complete' )
),
),
));
The tax_query array should be added to the main query array.

Excluding a Taxonomy From Post Type Loop Separated by Categories

I'm trying to display a post type’s categories and output the posts within that category, but I have another taxonomy that I want to exclude. This code is showing all Taxonomies and Terms registered to the specific post type.
The second taxonomy that I am trying to exclude is named "manufacturer" and is registered to multiple post types. I have tried doing 'operator'=>'NOT EXISTS' as well as what you see below, with the if...continue but nothing seems to work !!
<?php
$post_type = 'equipment';
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );
foreach( $terms as $term ):
if ( $term->slug == 'manufacturer' )
continue;
?>
<div class="row">
<div class="col-xs-12">
<h2><?php echo $term->name; ?></h2>
</div>
<div class="col-xs-12">
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<h4><?php echo get_the_title(); ?></h4>
<?php endwhile; endif; ?>
</div>
</div>
<?php endforeach;
endforeach; ?>
)
$args = array(
'post_type' => array('equipment'),
'posts_per_page' => -1,
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'manufacturer',
'field' => 'slug',
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );

Override 'Blog pages show at most' and show all posts for custom post type

I'm trying to overwrite the default 'Blog pages show at most' which is set to 5 posts. I have a custom post type called 'FAQs', which has the argument 'posts_per_page' => 999 in the query for getting all the posts of this type, however I can't seem to override the default restriction in the WordPress setting. My code for the FAQ query is below, which works on my local machine (MAMP) but not when I upload it to live. how do I show all posts of that type?
<?php
wp_reset_query();
// Query for getting custom taxonomy 'FAQ Type' of custom post type 'FAQs'
$cat_args = array (
'taxonomy' => 'faq_type',
'exclude' => array(12),
'posts_per_page' => 999,
//'show_all' => true,
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_categories ( $cat_args );
foreach ( $categories as $category ) {
//wp_reset_query();
$cat_query = null;
// Query for getting posts of custom post type 'FAQs'
$args = array (
'post_type' => 'faq',
'faq_type' => $category->slug,
'posts_per_page' => 999,
//'show_all' => true,
'orderby' => 'simple_page_ordering_is_sortable',
);
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) { ?>
<?php echo "<h2>". $category->name ."</h2>"; ?>
<ul id="resident-accordion" class="accordion white-bg-accordion" data-accordion data-allow-all-closed="true" role="tablist">
<?php
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
?>
<li class="accordion-item faq-content <?php //if ($firstLoop === true) { echo "is-active"; }?>" data-accordion-item>
<?php the_title(); ?>
<div class="accordion-content" data-tab-content>
<?php the_content(); ?>
</div>
</li>
<?php
} //wp_reset_query();
wp_reset_postdata(); //End WHILE
echo "</ul>";
} //End IF
wp_reset_postdata();
//wp_reset_query();
} //End FOR
?>
You can try to use this code below :
<?php
$cat_args = array(
'taxonomy' => 'faq_type',
'exclude' => array(7),
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_terms( $cat_args );
foreach ( $categories as $category )
{
$args = array(
'post_type' => 'faq',
'posts_per_page' => -1, // load all posts
'orderby' => 'simple_page_ordering_is_sortable',
'tax_query' => array(
array(
'taxonomy' => 'faq_type',
'field' => 'slug',
'terms' => $category->slug
)
)
);
$cat_query = new WP_Query( $args );
// enter the rest of your code below
}
Or you can use get_posts() to receive posts list.
<?php
$cat_args = array(
'taxonomy' => 'faq_type',
'exclude' => array(7),
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_terms( $cat_args );
foreach ( $categories as $category )
{
$posts = get_posts(array(
'numberposts' => -1, // get all posts.
'tax_query' => array(
array(
'taxonomy' => 'faq_type',
'field' => 'slug',
'terms' => $category->slug,
'operator' => 'IN',
),
),
'post_type' => 'faq',
));
// enter the rest of your code below
}
Cheers!
You found your answer by now I hope, but you were so close.
Instead of:
'posts_per_page' => 999,
try:
'posts_per_page' => -1,
see: Pagination Parameters

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