Display most recent post of each taxonomy for a CPT - php

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
)
)

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();
}?>

How to group woocommerce products by category in WordPress

I have at least 4 Parent Categories and each parent category has a sub-category.
The categories in WordPress looks like this:
Lidingö (Parent Category)
Direktåtkomst Förrådslänga(SubCategory)
7 kvm (product)
6 kvm (product)
Entréplan (SubCategory)
1 kbm (product)
1.5 kvm (product)
Nacka (Parent Category)
Sample(SubCategory)
aa (product)
bbb (product)
I want to query the products in WordPress and they should be grouped by categories.
This is my current code:
<?php
$args = array(
'post_type' => 'product',
array(
'taxonomy' => 'product_cat'
),
'posts_per_page' => 6,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
echo woocommerce_template_single_title();
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
I think the code above is not correct because the same products are being displayed for every pagination.
Do you have any idea what is the correct query to group all woocommerce products by category? Thanks
First you need to overwrite this woocommerce template to your current theme as you have to use custom loop to get products by category.
Just copy that template of woocommerce to your current_theme->create folder name (Woocommerce) -> Paste template to that folder.
Use Below Code:
$parent_terms= get_terms( array( 'taxonomy' => 'product_cat', 'parent' => 0 ) );
if($parent_terms= )
{
foreach( $parent_terms as $parent_term )
{
$child_terms = get_terms( array( 'taxonomy' => 'product_cat', 'parent' => $parent_term->term_id ) );
if($child_terms)
{
foreach( $child_terms as $child_term )
{
$product_args=
array(
'posts_per_page' => 50,
'post_type' => 'my_custom_type',
'posts_per_page' => 6,
'cat' => $child_term->term_id
);
$product_query = new WP_Query( $product_args );
while($product_query->have_posts()) : $product_query->the_post();
{
Here you will get product detail so you can display product details as you wanted
}
}
}
}
}
Your query is asking for all products that belong to the product category taxonomy - that is, all of them.
You need to narrow the search by adding the category you wish to search to the arguments. For example, if you wanted to search via the category slug, you would use:
$args = array(
'post_type' => 'product',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'category-slug1'
),
'posts_per_page' => 6,
);
To loop through all the categories on one page, you'll want something like this:
$my_categories = get_terms( 'TERM_NAME_HERE' );
$my_categories_count = count( $my_categories );
if ( $my_categories_count > 0 && is_array( $my_categories ) ) {
echo '<div class="wrap">';
foreach ( $my_categories as $single_cat ) { ?>
<h2><?php echo $single_cat->name; ?></h2>
<?php
$cat_posts_args = array(
'post_type' => 'product',
'order' => 'ASC',
'orderby' => 'date',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $single_cat->term_id,
'include_children' => false
)
)
);
$cat_posts = new WP_Query( $cat_posts_args );
if ( $cat_posts->have_posts() ) :
echo '<p>';
while ( $cat_posts->have_posts() ) : $cat_posts->the_post(); ?>
<span><?php the_title(); ?></span>: <?php echo get_the_excerpt(); ?><br>
<?php endwhile;
echo '</p>';
else :
if ( !$parent ) echo '<p>No products found.</p>';
endif;
wp_reset_postdata();
} // end foreach
echo '</div>';
}

Retrieve wp_terms by category_name

I'm trying to limit the retrieved testimonial (using get_terms) by their category names.
category_name = "xxx" doesn't seem to do anything so I'm at a loss.
function testimonial_shortcode( $atts ) {
$cat = $atts['cat'];
$testim='<div id="owl-demo" class="owl-carousel owl-theme">';
$terms = get_terms( array(
'taxonomy' => 'testimonial',
'category_name' => $cat,
'hide_empty' => true,
) );
foreach($terms as $custom_texonomy){
$imageid=get_option("testimonial_".$custom_texonomy->term_id."_testimonials__image");
$imgurl=wp_get_attachment_image_src( $imageid, 'full');
$testim.=' <div class="item">
...
}
add_shortcode( 'testimonialcat', 'testimonial_shortcode' );
'category_name' isn't a valid argument for get_terms.
The WP reference documentation here
lists the acceptable arguments. The one you want is:
'name': (string|array) Optional. Name or array of names to return term(s) for.
So, assuming $cat is the name you want to search for, try changing your get_terms arguments to:
$terms = get_terms( array(
'taxonomy' => 'testimonial',
'name' => $cat,
'hide_empty' => true,
) );
Update:
get_terms only returns information about the term you searched for, as per your original question.
To get all posts associated with a term, you need to use get_posts and tax_query as follows:
$myposts = get_posts(array(
'showposts' => -1, // get all posts
'post_type' => 'post', // change to whatever post type you want, or leave out if you want to get all post types
'tax_query' => array(
array(
'taxonomy' => 'testimonial',
'field' => 'name',
'terms' => $cat
))
));
Reference: get_posts documentation in WP Codex
category_name is not a valid arg for get_terms, try 'name' instead:
function testimonial_shortcode( $atts ) {
$cat = $atts['cat'];
$testim='<div id="owl-demo" class="owl-carousel owl-theme">';
$terms = get_terms( array(
'taxonomy' => 'testimonial',
'name' => $cat,
'hide_empty' => true,
) );
foreach($terms as $custom_texonomy){
$imageid=get_option("testimonial_".$custom_texonomy->term_id."_testimonials__image");
$imgurl=wp_get_attachment_image_src( $imageid, 'full');
$testim.=' <div class="item">
...
}
add_shortcode( 'testimonialcat', 'testimonial_shortcode' );

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

Categories