Exclude Draft Post in related Post Wordpress - wordpress

I am currently using a custom code for related post filtered by category that displays 4 related posts.
My code works fine except it also displays the draft posts. which ideally it should and is a bit frustrating. Here is my code for the related post.
<div class="relatedposts">
<?php
// get current post categories and tags
$categories = get_the_category($post->ID);
$tags = get_the_tags($post->ID);
if ($categories || $tags) {
$category_ids = array();
if($categories)
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$tag_ids = array();
if($tags)
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $category_ids
),
array(
'taxonomy' => 'post_tag',
'field' => 'id',
'terms' => $tag_ids
)
),
'post__not_in' => array($post->ID),
'posts_per_page'=> 4, // Number of related posts that will be shown.
);
// query posts
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
?>
<div class="related-post-title">
<?php
echo "<h2>Related Health Hub Articles</h2>";
?>
</div>
<?php
while( $my_query->have_posts() ) { $my_query->the_post();
// display each post
?>
<div class="related-post-thumb col-sm-3">
<a href='<?php the_permalink(); ?>' rel='canonical' class="related-wrapper">
<div class="related-thumb"><?php the_post_thumbnail(array(150,100)); ?></div>
<h4 class="related-title"><?php the_title();?></h4>
</a>
</div>
<?php
}
}
}
wp_reset_postdata();
?>
</div>

I have reviewed your code. you have missed the post_status column. Anything with the status "publish" is what you want.
Please add 'post_status' => 'publish' in $args array.
You can see built-in WordPress functions.
http://codex.wordpress.org/Integrating_WordPress_with_Your_Website

Try this code
<div class="relatedposts">
<?php
// get current post categories and tags
$categories = get_the_category($post->ID);
$tags = get_the_tags($post->ID);
if ($categories || $tags) {
$category_ids = array();
if($categories)
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$tag_ids = array();
if($tags)
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $category_ids
),
array(
'taxonomy' => 'post_tag',
'field' => 'id',
'terms' => $tag_ids
)
),
'post__not_in' => array($post->ID),
'posts_per_page'=> 4, // Number of related posts that will be shown.
'post_status' => 'publish'
);
// query posts
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
?>
<div class="related-post-title">
<?php
echo "<h2>Related Health Hub Articles</h2>";
?>
</div>
<?php
while( $my_query->have_posts() ) { $my_query->the_post();
// display each post
?>
<div class="related-post-thumb col-sm-3">
<a href='<?php the_permalink(); ?>' rel='canonical' class="related-wrapper">
<div class="related-thumb"><?php the_post_thumbnail(array(150,100)); ?></div>
<h4 class="related-title"><?php the_title();?></h4>
</a>
</div>
<?php
}
}
}
wp_reset_postdata();
?>
</div>

$search_query=get_search_query();
function __extra_where($sql){
global $wpdb;
return ' AND '.$wpdb->prefix.'posts.post_status="publish" AND ( 1=1 '.$sql.' ) ';
}
add_filter( 'posts_where', '__extra_where', 10, 2 );
$query = new WP_Query(array(
'post_status' => array( 'publish' ),
'post_type' => ['post'/*your post_type here */],
's'=>$search_query,
'order' => 'ASC'
));
remove_filter( 'posts_where', '__extra_where', 10 );

Related

How to get posts from multiple custom post types based on many taxonomy terms

I want to be able to get posts from different custom post types based on multiple taxonomy terms.
For example i have a post type called cme-education media gallery and post and I wish to get posts returned from them that matches the taxonomy terms called covid-19 and stroke.
I tried the below code to get the posts individually for each custom post type and then merge those which I can then then use in a wp_query to return the results.
$currentID = get_the_ID();
$test_type = get_test_type($post);
$post_terms = get_terms(array("covid-19", "stroke"));
$postTerm = [];
foreach ($post_terms as $post_term) {
$postTerm[] = $post_term->slug;
} ?>
<?php if (!empty($postTerm)) { ?>
<div class="recommended-activities-section template-in-conversation">
<?php
$first_post_ids = get_posts( array(
'fields' => 'ids', // only return post ID´s
'posts_per_page' => '1',
'orderby' =>'rand',
'post_type' => 'post',
'tax_query' => array (
array (
'taxonomy' => 'vocabulary_1',
'field' => 'slug',
'terms' => $postTerm,
'operator' => 'IN',
'order' => 'ASC'
)
)
));
$second_post_ids = get_posts( array(
'fields' => 'ids', // only return post ID´s
'posts_per_page' => '-1',
'orderby' =>'rand',
'post_type' => 'cme-education',
'tax_query' => array (
array (
'taxonomy' => 'vocabulary_1',
'field' => 'slug',
'terms' => $postTerm,
'operator' => 'IN',
'order' => 'ASC'
)
)
));
$third_post_ids = get_posts( array(
'fields' => 'ids', // only return post ID´s
'posts_per_page' => '-1',
'orderby' =>'rand',
'post_type' => array('media_gallery'),
'tax_query' => array (
array (
'taxonomy' => 'vocabulary_1',
'field' => 'slug',
'terms' => $postTerm,
'operator' => 'IN',
'order' => 'ASC'
)
)
));
$merged_post_ids = array_merge( $first_post_ids, $second_post_ids, $third_post_ids);
$wp_query = new WP_Query( array(
'post_type' => 'any', // any post type
'post__in' => $merged_post_ids, // our merged queries
'posts_per_page' => '3',
'orderby' =>'rand',
) ); ?>
<div class="activity-container">
<?php if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="recommended-activities-wrap js-recommended-activity-slide">
<div class="image-wrapper">
<?php the_post_thumbnail("in-convo"); ?>
</div>
<div class="vertical-2-col-split">
<div class="recommended-content-top">
<h3 class="activity-type">
<?php $general = get_field('general');
$activity_type = $general['activity_type'];
$cme = $general['cme_accredited'];
if( $activity_type ) { ?>
<?php echo $activity_type;?>
<?php } else if ( 'media_gallery' == get_post_type() ) {
echo "CONFERENCE HUB";
} else {
echo "JOURNALS";
} ?>
</h3>
<h4 class="activity-title dark-blue-text"><?php the_title(); ?></h4>
<?php if ( 'cme-education' == get_post_type() ) { ?>
<span class="accreditation-type"><i class="fa-thin fa-award"></i> This activity is CE/CME accredited</span>
<?php } ?>
</div>
<a class="c-button mb-0 dark-blue-bg" href="<?php the_permalink() ?> " target="_blank">
<?php if ('media_gallery' == get_post_type() ) {
echo "GO TO HUB";
} else if ('cme-education' == get_post_type()) {
echo "LAUNCH ACTIVITY";
} else {
echo "GO TO ARTICLE";
} ?>
</a>
</div>
</div>
<?php endwhile;
// reset after query
wp_reset_query();
else :
echo 'Sorry, no posts matched your criteria.';
endif; ?>
</div>
</div>
<?php } else {
echo "";
}

How to show custom texonomy's category related post on single post page

I am working with a custom post type name= product and I have a custom taxonomy name= product-category
on the taxonomy-product.php I am showing a single product and related product based on its single product taxonomy term. But something is missing I can't figure it out.
here is my code.
<?php
//related products
$terms = get_the_terms( $post->ID , 'product-category', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product-category',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'AND' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 3,
'ignore_sticky_posts' => 1,
'orderby' => 'rand',
'post__not_in'=>array($post->ID)
) );
if($second_query->have_posts()) {
while ($second_query->have_posts() ) : $second_query->the_post(); ?>
<div class="single_related">
<?php if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail( 'related_sm', array('alt' => get_the_title()) ); ?>
<?php } else { ?>
<?php the_title(); ?>
<?php } ?>
</div>
<?php endwhile; wp_reset_query();
}?>
I have also tried
https://www.banna360.com/display-related-post-product-based-taxonomy-wordpress/
https://code.tutsplus.com/tutorials/show-wordpress-related-posts-with-taxonomy-and-a-custom-post-type--cms-32303

Display posts based on CPT category Name

I have post type named 'service'. It has two categories 'Left' & 'Right' I wish to display all posts of 'Left' category. Here's my code
<?php
$args = array (
'post_type' => 'service',
'post_status' => 'publish',
'order' => 'ASC',
'category_name' => 'Left',
'posts_per_page'=>-1
);
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
//$image11 = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
?>
<li><i class="fa fa-file-text"></i> <?php the_title(); ?></li>
<?php } } wp_reset_postdata(); ?>
The above code returns nothing.
Please try with below:
On the $args Array please use the Left category ID insted of category_ID on "category" column.
<?php
$args = array (
'post_type' => 'service',
'post_status' => 'publish',
'order' => 'ASC',
'category' => 'category_ID',
'posts_per_page' =>-1
);
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
//$image11 = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
?>
<li>
<a href="<?php the_permalink(); ?>">
<i class="fa fa-file-text"></i>
<?php the_title(); ?>
</a>
</li>
<?php
}
}
wp_reset_postdata();
?>
Try this with the category name
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category', //name of texonomy
'field' => 'slug',
'terms' => 'Left'
)
)
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
//your code
endwhile;
endif;

How can I get only those categories which has products on sale?

I am trying to query woocommerce product categories but only those which have on sale products. Is there any possibilities? The result is hierarchy parent > child. I want to show both parent and its child. i.e. if child has product on sale print the parent category as well.
Here is the code I wrote so far
<ul class="accordion list-group sub-catalog">
<?php $terms = get_terms('product_cat', array( 'parent' => 0, 'exclude' => '15' ));
if( $terms ):
$original_query = $wp_query;
foreach ( $terms as $key => $term ):
$child = get_terms(
'product_cat',
array(
'child_of' => $term->term_id,
'hide_empty' => true
)
);
?>
<li class="accordion-card list-group-item">
<div class="acc-card-title">
<?php echo $term->name; ?>
<?php if ( ! $child ){ ?>
<?php
} else {
?>
<span class="fa fa-plus"></span>
<?php
}
?>
</div>
<ul class="accordion list-group sub-catalog">
<?php
$child_terms = get_terms(
'product_cat',
array(
'child_of' => $term->term_id,
'hide_empty' => true
)
);
foreach ( $child_terms as $child_term ) {
$re_child_terms = get_terms(
'product_cat',
array(
'child_of' => $child_term->term_id,
'hide_empty' => true
)
);
if ( ! $re_child_terms ){
?>
<li class="accordion-card list-group-item">
<div class="acc-card-title">
<?php echo $child_term->name; ?>
</div>
</li>
<?php
}
}
?>
</ul>
</li>
<?php
endforeach;
$wp_query = null;
$wp_query = $original_query;
?>
</ul>
<?php endif; ?>
Thanks in advance.
<ul class="accordion list-group sub-catalog">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'rand',
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
$terms = [];
while ( $loop->have_posts() ) : $loop->the_post();
$terms = array_diff($terms,wp_get_post_terms( get_the_id(), 'product_cat',['fields'=>'ids'] ) );
$term = reset($terms);
?>
<li class="accordion-card list-group-item">
<div class="acc-card-title">
<?php echo $term->name; ?>
</div>
</li>
<?php
endwhile;
} else {
echo __( '' );
}
wp_reset_postdata();
?> </ul>
this is how I get the on sale products and get their category name . where to use array_diff function
The code below solved the problem.
<ul class="accordion list-group sub-catalog">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'rand',
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
$alreadyDisplayed = [];
while ( $loop->have_posts() ) : $loop->the_post();
$term_list = wp_get_post_terms($post->ID, 'product_cat', array("all"));
foreach($term_list as $term_single) {
if ( !in_array( $term_single->name, $alreadyDisplayed) ) {
echo ' <li class="accordion-card list-group-item">
<div class="acc-card-title"><a href="'.get_term_link($term_single).'" class="salecats">';
echo $term_single->name;
echo '</a><br></div>
</li>';
$alreadyDisplayed[] = $term_single->name;
}
}
?>
<?php
endwhile;
} else {
echo __( '' );
}
wp_reset_postdata();
?> </ul>

Taxonomy images from taxonomy list in WordPress

I want to list all categories of my custom post type with its images. I tried many codes, but I can't get it working. With this given code I'm getting all categories as a list, but I'm not getting the value of my custom field. Someone please help me with this.
$post_type = 'product';
$tax = 'productcat';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args=array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$term_id = $tax_term->term_id;
$term_meta = get_option( 'taxonomy_' . $term_id );
$my_cf = $term_meta[ 'category_image' ];
echo $my_cf;
}
}
I think here is the answer for this problem.
<?php
foreach((get_the_category()) as $category) {
echo '<img src="http://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';
}
?>
For more information please click here.
Another answer for it, it is also here. it might be useful for you.
<?php
$args = array( 'type' => 'product',
'child_of' => 16,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'product_cat',
'pad_counts' => false
);
$categories = get_categories( $args );
foreach($categories as $category):
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
?>
<div class="col-xs-6 col-sm-4 product-box"><img src="<?php echo $image; ?>"></div>
<?php
endforeach;
?>
Thank you so much for your code,
I got answer using you code, i did some modification also. this is the code
<div class="row">
<?php
$post_type = 'product';
$tax = 'productcat';
$args = array( 'type' => 'product',
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'productcat',
'pad_counts' => false
);
$categories = get_categories( $args );
foreach($categories as $category):
$term_id = $category->term_id;
$variable = get_field('category_image', 'productcat_'.$term_id);
$imageURL = $variable['sizes']['medium'];
?>
<div class="col-sm-4 alt-no-padding-r-x">
<div class="products_item">
<div class="product_head">
<p><?php echo $category->name; ?></p>
</div>
<div class="top-img">
<img src="<?php echo $imageURL; ?>" />
</div>
<div class="product_desc">
<p><?php echo $category->description; ?></p>
</div>
<div class="product_list text-left no-decor">
<ul>
<?php
$args=array(
'post_type' => $post_type,
"$tax" => $category->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();?>
<li><?php the_title(); ?> </li>
<?php endwhile;
}
?>
</ul>
</div>
<div class="readmore-product no-decor">
Read More
</div>
</div>
<div class="plan-shadow"></div>
</div>
<?php
endforeach;
wp_reset_query();
?>
</div>

Resources