How to display all posts assigned the same category? - php

I have a product category called Cable, and I want to grab all posts assigned to this category and output the title of said posts. This category is from the custom taxonomy 'Category' that woo-commerce adds, I'm not sure if that makes things harder? but I haven't found a solution yet.
Could anyone assist with this?

Try this code:
And modify as per your requirements:
add_shortcode( 'specific_category_posts', 'kb_get_posts' );
function kb_get_posts() {
$query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( '18' ), //Your custom category id
),
),
)
);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
}
}
wp_reset_postdata();
}

Use this code to show all posts assigned to the category cable
<?php
global $post;
$args = array( 'numberposts' => 10, 'category_name' => 'cable' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post);
?>
<div>
<?php
the_title();
the_excerpt(); ?>
</div>
<?php endforeach; ?>

I have created with list
add_shortcode( 'pi_cable_posts', 'pi_posts' );
function pi_posts() {
$pi_query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1, //--1 for unlimited and number to limit
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( 'YOUR ID HERE' ), //ADD YOUR ID HERE
),
),
));
if ( $pi_query ->have_posts() ) {
echo '<ul>';
while ( $pi_query ->have_posts() ) {
$pi_query ->the_post();
echo '<li><h2><a href="'.get_the_permalink().'">' . get_the_title() . '</h2></li>';
}
echo '</ul>';
}
wp_reset_postdata();}

You can use WP_Query() with tax_query().
EDIT:
Try out this code to get products on category page.
add_shortcode('product_list', 'zillion_show_products_title_by_cat', 10);
function zillion_show_products_title_by_cat()
{
if(!is_product_category()){
return;
}
$term = get_queried_object();
ob_start();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $term->term_id, // 21 is category id When you have more term_id's seperate them by comma.
'operator' => 'IN'
)
);
$the_query = new WP_Query($args);
// The Loop
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
echo '<h3>' . get_the_title() . '</h3>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
$contents = ob_get_clean();
return $contents;
}

Related

Get Category name/slug for wp_Query on product-category to show product list according to category

I am developing a woocommerce website, and i want to show product list according to a category on the product-category page i.e: www.wesiteName.com/product-category/ladies. I want to show all the ladies products on this link but how? My code didn't work.
I am using this function woocommerce_page_title() to get category name but it print the category name but didn't pass name in wp query don't know why.
if (is_product_category()) :
$title = woocommerce_page_title();
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'product_cat' => $title,
'orderby' => 'post_title',
'order' => 'DESC',
);
$loop = new WP_Query( $args );
endif;
It shows all the products from all categories. I just want to show a particular product list according to a category.
Try this code.
$args = array(
'number' => $number,
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
echo '<h4><a href="' . get_term_link( $product_category ) . '">' .
$product_category->name . '</a></h4>';
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
// 'terms' => 'white-wines'
'terms' => $product_category->slug
)
),
'post_type' => 'product',
'orderby' => 'title,'
);
$products = new WP_Query( $args );
echo "<ul>";
while ( $products->have_posts() ) {
$products->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php
}
echo "</ul>";
}
}
Hope its working for you

How to show related posts by category from different post type

In my website, I have two different post-types. One of them is publication with custom category-type as publication-category and the other is service with custom category-type as service-category. I am publishing some brochures in Publication page those are under different services. What I am trying to do is displaying these brochures in Services page by same category type. It is like, if the brochure is published by Education services, then this brochure should be displayed in Education service page.
I am currently using ACF plugin to do so, but whenever there is new brochure, I have to go to service page and add it there. Today I tried below code but it displays all brochures from different category-types not the same category-type.
Perhaps you guys can help me how I can arrange the code a way that works for my above request.
<?php
$custom_taxterms = wp_get_object_terms(
$post->ID,
'publication-category',
array( 'fields' => 'ids' )
);
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'publication-category',
'field' => 'id',
'terms' => $custom_taxterms
)),
'post__not_in' => array( $post->ID ),
);
$related_items = new WP_Query( $args );
if ( $related_items->have_posts() ) :
echo '<ul>';
while ( $related_items->have_posts() ) : $related_items->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
endif;
wp_reset_postdata();
?>
If you're on services page, so why do you use the 'publication-category' in
wp_get_object_terms(
$post->ID,
'publication-category',
array( 'fields' => 'ids' )
);
Seems like you have to use
$custom_taxterms = get_the_terms($post->ID, 'service-category');
$terms = [];
foreach ($custom_taxterms as $term) {
$terms[] = $term->slug
}
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'publication-category',
'field' => 'slug',
'terms' => $terms
)),
);
And create same slugs for both terms in both categories. From my understanding. It's quite difficult to understand your architecture
I find the solution by changing service-category taxonomy to publication-category as same with other post-type and creating relationship with below code from : https://wordpress.stackexchange.com/questions/139571/display-posts-with-same-taxonomy-term?rq=1
Thanks everyone
<?php
//get the post's venues
$custom_terms = wp_get_post_terms($post->ID, 'publication-category');
if( $custom_terms ){
// going to hold our tax_query params
$tax_query = array();
// add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
if( count( $custom_terms > 1 ) )
$tax_query['relation'] = 'OR' ;
// loop through venus and build a tax query
foreach( $custom_terms as $custom_term ) {
$tax_query[] = array(
'taxonomy' => 'publication-category',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
// put all the WP_Query args together
$args = array( 'post_type' => 'publication',
'posts_per_page' => 20,
'tax_query' => $tax_query );
// finally run the query
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="listing-title"><?php the_title(); ?></div>
<div class="listing-image">
</div>
<?php
endwhile;
}
wp_reset_query();
}?>

Display excerpt, thumbnail and title of custom post type grouped by taxonomies?

I would like to display e.g. the title of a all posts with a custom post type and a specific taxonomy.
I tried following:
global $post;
$myposts = get_posts(array(
'post_type' => 'my-post-type',
'tax_query' => array(
array(
'taxonomy' => 'post-type-category',
'terms' => 'new-posts')
)
)
);
foreach ($myposts as $mypost) {
echo $mypost->post_title;
}
you can use WP Query :
<?php
$args = array(
'post_type' => 'my-post-type',
'tax_query' => array(
array(
'taxonomy' => 'post-type-category',
'field' => 'slug',
'terms' => 'new-posts',
),
),
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title() ; // title
the_excerpt(); // excerpt
the_post_thumbnail(); // post thumbnail
}
wp_reset_postdata();
} else {
// no posts found
}

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

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