Wordpress Custom Post Loop can't exclude category - php

I am trying to exclude a category from a custom post type loop but with no luck. I have no idea why this isn't working. I have looked all over the internet but no luck, please can someone help identify the issue.
I have a category with an ID of 141.
I am running through a loop of posts where I am trying to exclude posts assigned to that category but they are still showing up in the loop. Where am I going wrong? Here is my code.
<?php
$args = array(
'post_type' => 'programme',
'cat' => -141,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="grid-item">
<?php the_title(); ?>
</div>
<?php
endwhile;
else:
_e( 'Sorry, no posts matched your criteria.', 'textdomain' );
endif;
wp_reset_postdata();
?>

For anyone with a similar issue, I managed to fix it by using this code instead.
$args = array(
'post_per_page' => '-1',
'post_type' => 'programme',
'tax_query' => array(
array(
'taxonomy' => 'ticket-category',
'field' => 'slug',
'terms' => array( 'savers-tickets' ),
'operator' => 'NOT IN',
)
),
);

Related

How to specify a query for a custom post type using the selector advanced custom field?

I'm trying to filter through my 'Campaigns' custom post type and only display the post which has the select advanced custom fields set to 'Featured Campaign'. The select acf can only have a value of 'Featured Campaign' or 'Not a Featured Campaign'
So far this is my code, but instead of displaying the 'Campaign' with the 'Featured Campaign' select, it shows the most recently uploaded 'Campaign'
Any help would be appreciated. Thanks in advance!
<?php
$args = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'campaigns',
'meta_query' => array (
'key' => 'featured',
'value' => 'Featured Campaign'
)
);
query_posts( $args );
if (have_posts()) :
while (have_posts()) : the_post();
?>
<div class="post">
<?php the_post_thumbnail(); ?>
<h3 class="post__title heading--primary u-uppercase"><?php the_title(); ?></h3>
<p class="text-color--primary"><?php the_field(campaign_category); ?></p>
</div>
<?php
endwhile;
wp_reset_query();
endif;
?>
Thanks to CBroe in the comments section, the answer is as follows:
meta_query expects nested arrays, even in the case of one query. The $args array has been changed to and now works as expected:
$args = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'campaigns',
'meta_query' => array (
array (
'key' => 'featured',
'value => 'Featured Campaign'
)
)
);

Exclude a product category from the loop in Woocommerce

I'd like to exclude the category of my current post from the loop. Pretty easy usually, this time it doesn't work and I can't figure out what's wrong here.
Here's my page'code:
$postid = get_the_ID(); // curret product ID
<section class="related products">
<?php
$args = array(
'post__not_in' => array($postid), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'cat' => '-33' // Exclude cat
);
$related_products = new WP_Query($args);
?>
<h2><?php esc_html_e( 'Related products' ); ?></h2>
<div class="owl-carousel rigid-owl-carousel" >
<?php if( $related_products->have_posts() ) {
while( $related_products->have_posts() ) : $related_products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile; }
?>
</section>
End of page
<?php
wp_reset_postdata();
?>
It shows all products (except the displayed one, which is correct).
Do you have any suggestion?
Try with the following additional tax_query, as product categories are a custom taxonomy:
<?php
$related_products = new WP_Query( array(
'post__not_in' => array( get_the_ID() ), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array( 33 ), // HERE the product category to exclude
'operator' => 'NOT IN',
) ),
) );
if( $related_products->have_posts() ) : ?>
<h2><?php esc_html_e( 'Related products' ); ?></h2>
<div class="owl-carousel rigid-owl-carousel" >
<?php
while( $related_products->have_posts() ) : $related_products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
?>
</div>
<?php endif; ?>
You can get current post's category using get_the_category
and you can exclude categories using category__not_in in your argument.
So your argument should be like bellow
$args = array(
'post__not_in' => array($postid), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'category__not_in' => get_the_category(get_the_ID())//exclude category of current post
);
Try this then let me know the result. Thanks

display custom post that has two taxonomies

hey i'm trying to display posts that share term from two different taxonomies, but for some reason i just displays all posts. Not sure how to get this working. any help would be awesome! below is the arguments im using for my wp_query.
thanks in advance.
$args = array(
'post_type' => $posttype,
'posts_per_page' => 99999,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'mytag',
'field' => 'slug',
'terms' => 'tag1',
),
array(
'taxonomy' => 'mycategory',
'field' => 'slug',
'terms' => 'cat1',
),
),
);
html
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();?>
<?php endif;?>
html here...
<?php endwhile; ?>
<?php else: ?>
<h2>No posts found</h2>
<?php endif;
die();
// Correct code you closed endif before endwhile
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();?>
html here...
<?php endwhile; ?>
<?php else: ?>
<h2>No posts found</h2>
<?php endif;
die();

Wordpress show posts from custom post type with a category

I am using the CPT UI plugin which i have created a custom post type with (called Knowledgebase and the taxonomy called knowledgebase-categories)
i am using this code to display posts:
<?php $query = new WP_Query( array('post_type' => 'knowledgebase', 'posts_per_page' => 20, 'category_name' => 'Cisco' ) ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title(); ?>
</article> <!-- .et_pb_post -->
<?php endwhile; ?>
it works fine without the category_name but with the above code its not showing any posts
there are posts with a category of Cisco
May this will Help you
<?php $query = new WP_Query( array(
'post_type' => 'knowledgebase',
'cat' => 5, // Whatever the category ID is for your aerial category
'posts_per_page' => 10,
'orderby' => 'date', // Purely optional - just for some ordering
'order' => 'DESC' // Ditto
) );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
Here you can achieve by this
$args = array(
'post_type' => 'knowledgebase',
'tax_query' => array(
array(
'taxonomy' => 'knowledgebase-categories',
'field' => 'slug',
'terms' => 'knowledgebase-terms',
),
),
);
$query = new WP_Query( $args );
because they are posts from a custom post type i had to change category_name to the taxonomy (which is knowledgebase-categories)

Custom Taxonomy WP_Query

I am trying to display a custom post type that has custom taxonomy, but I am not having any luck. Nothing is showing up. I appreciate any help.
Post type = gallery
Custom Taxonomy slug = photoarea
The 'photoarea' I want to display = fourth
<?php
$args = array(
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'photoarea',
'field' => 'fourth',
)
),
'posts_per_page' => 10,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_post_thumbnail();
endwhile; endif;
wp_reset_query();
?>
You may use below code snippet:
$the_query = new WP_Query( 'post_type=gallery&photoarea=fourth');
and then your while loop.
if my understanding is right, that you need to get the custom taxonomy with following code,
instead of field you must use term to get the posts in Fourth
<?php
$args = array(
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'photoarea',
'field' => 'slug',
'terms' => 'fourth'
)
),
'posts_per_page' => 10,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_post_thumbnail();
endwhile; endif;
wp_reset_query();
?>
$args = array('post_type' => 'gallery','posts_per_page'=>'-1','tax_query' => array(array(
'taxonomy' => 'photoarea',
'field' => 'term_id',
'terms' => $your_term_id,
),
),
);

Categories