This is what I have:
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'directory-category',
'pad_counts' => false
);
Which gets me the categories.
What I want is to get the child categories of this directory-category taxonomy.
Any ideas on how to do that?
I'm not asking for a solution, just an advice or someone to show me the road.
Googling didn't help :/
Here is a screenshot HERE
You said you didn't want a direct answer, but essentially you want to use get_terms found here:
https://developer.wordpress.org/reference/functions/get_terms/
SELECT * from prod_term_taxonomy WHERE parent = 0;
UPDATE:
// Using your specific parent taxonomy id of 214 the query is below
global $wpdb;
$results = $wpdb->get_results("SELECT * from prod_term_taxonomy WHERE taxonomy = 'directory-category'");
// then you can use WordPress get_term to query each object to get it's name based on it's term_id. $results will be an array of objects so you will use a foreach loop to loop through to get each $result like this...
$child_cat_array = array();
foreach ($results as $result) {
$term = get_term( $result->term_id, $taxonomy );
$name = $term->name;
// the slug will be used for querying for posts
$slug = $term->slug;
// this will push the slug of the child category into the array for querying posts later
array_push($child_cat_array, $slug);
}
You can then modify your get_posts query like this:
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'directory-category',
'field' => 'slug',
'terms' => $child_cat_array
)
)
);
$postslist = get_posts( $args );
If you want to display a list of Subcategories and the related posts based on a single category provided by you, you can use the following code. Make sure to use your own names of taxonomy, post_type and terms:
function ow_subcategories_with_posts_by_category( $taxonomy, $post_type, $term ) {
$category = get_term_by( 'slug', $term, $taxonomy );
$cat_id = $category->term_id;
// Get all subcategories related to the provided $category ($term)
$subcategories = get_terms(
array(
'taxonomy' => $taxonomy,
'parent' => $cat_id,
'orderby' => 'term_id',
'hide_empty' => true
)
);
?>
<div>
<?php
// Iterate through all subcategories to display each individual subcategory
foreach ( $subcategories as $subcategory ) {
$subcat_name = $subcategory->name;
$subcat_id = $subcategory->term_id;
$subcat_slug = $subcategory->slug;
// Display the name of each individual subcategory with ID and Slug
echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug . '</h4>';
// Get all posts that belong to this specific subcategory
$posts = new WP_Query(
array(
'post_type' => $post_type,
'posts_per_page' => -1, // <-- Show all posts
'hide_empty' => true,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => $subcat_id,
'field' => 'id'
)
)
)
);
// If there are posts available within this subcategory
if ( $posts->have_posts() ):
?>
<ul>
<?php
while ( $posts->have_posts() ): $posts->the_post();
//Show the title of each post with the Post ID
?>
<li>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></li>
<?php
endwhile;
?>
</ul>
<?php
else:
echo 'No posts found';
endif;
wp_reset_query();
}
?>
</div>
<?php
}
ow_subcategories_with_posts_by_category( 'name-of-your-taxonomy', 'name-of-your-post-type', 'name-of-your-specific-term' );
'name-of-your-taxonomy' is the name of the main taxonomy. e.g.: 'victual_category'
'name-of-your-post-type' is the name of your post type. e.g.: 'victual'
'name-of-your-specific-term' is the name of the specific category that you want to use so that subcategories that belong to that category can be displayed. e.g.: 'food'
So if I call the function:
ow_subcategories_with_posts_by_category( 'victual_category', 'victual', 'food' );
That would display all the subcategories with their respective Posts that belongs to Food of the Victual-Category taxonomy:
Subcategory: Appetizers - ID: 35 - Slug: appetizers
Post: Chips & Salsa - ID: 464
Post: Queso - ID: 465
Subcategory: Tacos - ID: 36 - Slug: tacos
Post: Al Pastor - ID: 466
Post: Fish Al Pastor - ID: 467
Related
after several unsuccessful searches, I ask my question here.
Indeed, I'm trying to display a list of posts grouped by categories:
CAT A
post1
post2
post3
CAT B
post4
post5
post6
post7
...
Here is the code I tried.
I can display the categories, but not the posts
<?php
$terms = get_terms( 'secteur', array(
'orderby' => 'count',
'hide_empty' => 0
) );
foreach( $terms as $term ) {
$args = array(
'post_type' => 'client',
'posts_per_page' => '-1',
'secteur' => $term->slug
);
$query = new WP_Query( $args );
echo'<h3>' . $term->name . '</h3>';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post();
$secteur_dactivite = get_field( 'secteur_dactivite' );
echo '<div class="cat-'.esc_html( $secteur_dactivite->slug ). '"><img src="'.get_field( 'logo' ).'"></div>';
endwhile;
wp_reset_postdata();
}
?>
You need to use tax_query as an WP query attribute, instead of secteur.
Try replacing that with:
$args = array(
'post_type' => 'client',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'secteur',
'field' => 'slug',
'terms' => $term -> slug,
),
),
);
thank you very much for your response.
Unfortunately, this does not change the display. The titles are displayed but not the articles.
<h3>CAT1</h3
<h3>CAT2</h3>
<h3>CAT3</h3>
If it helps, I can display all the items with the following code :
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'client',
'post_status' => 'publish'
));
if($posts)
{
echo '<div class="row all-item">';
foreach($posts as $post)
{
echo '<div"><img src="'.get_field( 'logo' ).'"></div>';
}
echo '</div>';
}
?>
Thank you very much for your answers.
But nothing has worked for me, and I can’t come up with any solutions.
I think the problem lies in the configuration of my taxonomy.
This is my custom post type configuration (client):
https://imgur.com/uDom3PH
my taxonomy configuration (secteur) :
https://imgur.com/WRwsSbR
my custom field (secteur_dactivite) :
https://imgur.com/NKQ4GPn
Thanks again for your help
I have a parent category which has a lot of child categories, which also have a lot of their child categories and these categories have posts. I want to show on page all the child categories titles from the first parent category and posts titles only once.
Now I have code which shows posts several times after every loop iteration, but I want to display it only once.
Here is my code snippet
# get child categories
$sub_cats = get_categories( array(
'child_of' => $parent_id,
'hide_empty' => 0
) );
if( $sub_cats ){
foreach( $sub_cats as $cat ){
echo '<h3>'. $cat->name .'</h3>';
# get posts from category
$myposts = get_posts( array(
'numberposts' => -1,
'category' => $cat->cat_ID,
'orderby' => 'post_date',
'order' => 'DESC',
) );
# show posts
global $post;
foreach($myposts as $post){
setup_postdata($post);
echo '<li class = "test">'. get_the_title() .'</li>';
}
}
wp_reset_postdata();
}
I'm not WordPress developer, but I need to do this task. I didn't write this code, I just found it on the internet.
Can someone help me to show posts only once or improve this code?
thank you
If you have duplicate post outputted:
It's certainly because post has multiple categories. So a single post will be found in multiple categories.
To prevent it, you could make a query for unique posts from your categories:
see:
//get terms from taxonomy category
$sub_cats = get_categories( [
'child_of' => $parent_id,
'hide_empty' => 0
] );
//wp-query post from your categories
$query = new WP_Query( [ 'category__in' => array_map( function ( $o ) {
return $o->term_id;
}, $sub_cats ) ] );
//only unique - never tested this
add_filter('posts_distinct', function(){return "DISTINCT";});
//your posts
$posts = $query->posts;
If you need to keep your loop as it is, you can use an array to store displayed post_ids and prevent duplicate to show:
# get child categories
$sub_cats = get_categories( array(
'child_of' => $parent_id,
'hide_empty' => 0
) );
$sub_cats_unique_posts = []; //array to store displayed post IDs
if( $sub_cats ){
foreach( $sub_cats as $cat ){
echo '<h3>'. $cat->name .'</h3>';
# get posts from category
$myposts = get_posts( array(
'numberposts' => -1,
'category' => $cat->cat_ID,
'orderby' => 'post_date',
'order' => 'DESC',
) );
# show posts
global $post;
foreach($myposts as $post){
if(!in_array($post->ID, $sub_cats_unique_posts)){ //if not displayed already
setup_postdata($post);
echo '<li class = "test">'. get_the_title() .'</li>';
$sub_cats_unique_posts[] = $post->ID; //add this post ID to displayed one
}
}
}
wp_reset_postdata();
}
This should only display unique posts from $parent_id child categories.
i need get all posts from subcategories like this:
1 - Main (id=7)
--1.1 category
--1.2 category
--1.3 category
Main category have id = 7, need ignore this category and get all post from subcategories without pagination.
First get the term children of the category:
$sub_cats = get_term_children( 7, 'category' );
This will give you and array if ids of sub-categories
Then use this array as tax query in arguments of wp_query:
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $sub_cats
),
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<p>' . get_the_title() . '</p>';
}
wp_reset_postdata();
} else {}
I was trying to create shortcode for custom taxonomy terms dynamically. But failing to do so.
Suppose, if there is a term called "wordpress" then I should be able to query all the posts associated with that term via shortcode.
To be more precise, suppose if there is a taxonomy called 'event' and under that taxonomy there are multiple terms. So, I was trying to query posts under each of the term via shortcode of each of the term.
Here is what I tried:
function wordpress_recent_post( $atts, $content ) {
$a = shortcode_atts( array(
'cat' => '',
), $atts );
$args = array(
'posts_per_page' => 1,
'offset' => 0,
'category_name' => $a['cat'],
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
);
$recent_posts = new WP_Query( $args );
ob_start();
if ( ! $recent_posts-> have_posts() ) {
return 'No Posts Found for ' . $a['cat'];
}
while ( $recent_posts->have_posts() ) {
$recent_posts->the_post();
the_title( '<h2>', '</h2>' );
if ( '' != $a['cat'] ) {
$href = '/category/' . $a['cat'];
} else {
$href = '/blog';
}
echo "<p><a href='$href'>Read More" . ucwords( $a['cat'] ) . '</a></p>';
}
wp_reset_query();
return ob_get_clean();
}
add_shortcode( 'wordpress_recent_post', array($this, 'wordpress_recent_post') );
And then I used this to call the posts from a term called "features" whose id is '183' (suppose)
[wordpress_recent_post cat="183"]
Any help would be really very appreciable.
Thanks!
Adding term slug did it. There should be slug not id, like this:
[wordpress_recent_post cat="features"]
I'm working on a project and getting same results in different category slug. Please help what I'm doing wrong here.
$act = $_POST['act'];
$args = array(
'posts_per_page' => 100,
'offset' => 0,
'category' => $act,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'product',
'taxnomy' => 'product_cat',
'post_status' => 'publish');
$myposts = get_posts( $args );
global $product;
global $wpdb;
foreach ($myposts as $key => $value) {
$id = $value->ID;
echo '<li class="product type-product status-publish has-post-thumbnail first grid with-hover add-hover open-on-mobile with-border span3 featured shipping-taxable product-type-simple product-cat-accommodation-durban product-cat-accommodation-battlefields product-cat-battlefields product-cat-comfortable-accommodation-durban product-cat-comfortable-accommodation-battlefields product-cat-durban instock">';
echo '<div class="product-wrapper">';
echo '<a class="thumb" href="'.get_permalink( $id ).'">';
$post_thumbnail_id = get_post_thumbnail_id($id);
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
echo '<img src="'.$post_thumbnail_url.'" />';
echo '</a>';
echo '<h3>'.get_the_title( $id ).'</h3>';
echo '</div></li>';
}
<?php
$slug = "category-b";
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $slug
)
),
'post_type' => 'product',
'orderby' => 'title',
);
$the_query = new WP_Query( $args );
foreach ($the_query->posts as $key => $value) {
print_r($value->ID);
}
?>
After careful consideration, I found the root of the problem:
The category needs to be an ID, quoting Wordpress:
Note: The category parameter needs to be the ID of the category, and
not the category name.
Note: The category parameter can be a comma separated list of
categories, as the get_posts() function passes the 'category'
parameter directly into WP_Query as 'cat'.
Note: The category_name parameter needs to be a string, in this case,
the category name.
Taxonomy is not well written, is "taxonomy" and not "taxnomy". Also, you can remove it from there as it's not filtering anything.