Remove posts with specific class name from related posts - WP - php

Some of my posts have a class name ‘disabled’ and I have to remove them from related posts because they have a style display: none so it inserts it but doesn’t display it.
How can I filter posts with a class name disabled and add them to 'posts__not_in' to avoid them?
<?php
$related = get_posts( array(
'numberposts' => 4,
'post__not_in' => array($post->ID)
) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>" class="card <?php if( get_field('public') == 'no') echo 'disabled' ?>">
<p><?php the_title();?></p>
</a>
<?php } wp_reset_postdata(); ?>

My recommendation would be to create a category and assign it to the posts. Then use a tax_query to filter out these posts from your get_posts(). Example below is Psuedo code:
$related = get_posts([
'post_type' => 'post',
'numberposts' => 4,
'post__not_in' => [$post->ID],
'tax_query' => [
[
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'DISABLED_CATEGORY_SLUG',
'operator' => 'NOT IN'
],
],
]);

Related

Show posts only from specific CPT Taxonomy Category

I have searched high and low and applied all of the solutions I've found here on Stackoverflow with no luck so trying a post to see if someone could assist me...
I am trying to display the posts that sit under a category in my custom post type taxonomy.
The CPT is called "Fabrics" and the taxonomy is called "Type" - Under "Type" I have a category called "Lycra"
I want to display all of the posts that exist under "Lycra"
Here is the code I have so far, but it just lists all of the posts under Fabrics:
<?php
$cat_terms = get_terms(
array(
'post_type' => 'fabrics',
'post_status' => 'publish',
'posts_per_page' => 9999999,
'orderby' => 'date',
'order' => 'DES',
'tax_query' => array(
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => 'lycra',
),
),
)
);
if( $cat_terms ) :
echo '<ul class="fabric-listing">';
foreach( $cat_terms as $term ) :?>
<li class="each-fabric">
<a href="<?php echo get_term_link( $term );?>">
<img src="<?php the_field('imagecat', $term); ?>">
<div class="term-name">
<?php echo $term->name; ?>
</div>
</a>
</li>
<?php
wp_reset_postdata();
endforeach;
echo '</ul>';
endif;
?>
What am I missing here? Any help is super appreciated
Use get_posts and not get_terms
$cat_terms = get_posts(
array(
'post_type' => 'fabrics',
'post_status' => 'publish',
'numberposts' => -1,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => 'lycra',
),
),
)

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.

Display most recent post of each taxonomy for a CPT

I'm trying to display the most recent posts from each custom taxonomy. The CPT is for 'member-services' and the taxonomy is 'services'.
Currently trying to combine two (Getting Custom Taxonomies and Getting Posts under Custom Taxonomy) scripts I've found online.
I'm unsure where I'm going wrong.
<!-- Add in list of member services -->
<?php // Get the taxonomy's terms
$terms = get_terms(
array(
'taxonomy' => 'services',
'hide_empty' => false,
)
);
// Check if any term exists
if ( ! empty( $terms ) && is_array( $terms ) ) {
// Run a loop and print them all
foreach ( $terms as $term ) { ?>
<a href="<?php echo esc_url( get_term_link( $term ) ) ?>">
<?php echo $term->name; ?>
</a>
<?php
$post_args = array(
'numberposts' => 5,
'post_type' => 'services',
'services' => $term->term_id,
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<?php the_title(); ?>
<?php
}
}
}
?>
Real world logic:
Show most recent member-services (CPT) offered under custom taxonomies - 'product', 'price', etc on home page.
Currently:
Shows list of custom taxonomies, so get_terms seems to be working.
Yes your get_terms is working but your get_posts is not working because you have passed taxonomy name in post_type.
I have done some changes in your code. May be this will work for you
<?php
$post_args = array(
'posts_per_page' => 5,
'post_type' => 'member-services',
'orderby' => 'date',
'sort_order' => 'desc',
'tax_query' => array(
array(
'taxonomy' => 'services',
'field' => 'id',
'terms' => $term->term_id,
'include_children' => false
)
)
);
$posts = get_posts($post_args);
?>
You have to write tax_query in get_posts() functions.
Just Replace
'services' => $term->term_id,
with
'tax_query' => array(
array(
'taxonomy' => 'services',
'field' => 'id',
'terms' => $term->term_id
)
)

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

exclude a custom taxonomy from wp_query

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;

Categories