Get all posts from children categories - wordpress

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 {}

Related

Wordpress query odererd by acf-field with title

I need help with a Wordpress query to get posts.
I have an ACF-numeric-Field called "year".
I need a query to get posts who have entered a year, and the year is echoed as title.
Someting like
1925
Post A
Post B
Post D
1926
Post C
Post E
This is waht I have so far to get posts with a "year" set and ordered by.
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'artikel',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year',
'compare' => 'EXISTS'
),
array(
'key' => 'year',
'value' => 1900,
'type' => 'NUMERIC',
'compare' => '>'
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
Can someone help me how to put the year as the title?
If $the_query has values than you can do this
// assuming that all posts are sorted by year. 1925, 1926, 1930 etc...
$the_query = new WP_Query($args);
// will contain all posts sorted by year
$posts_by_year = [];
// start looping through the posts
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
// get year ACF field
$year = get_field('year');
// check if $posts_by_year already has year, if not create and add the post, if exists just add the post
if (isset($posts_by_year[$year])) {
$posts_by_year[$year][] = get_post();
} else {
$posts_by_year[$year] = [
get_post()
];
}
}
}
wp_reset_postdata();
// check if we have any posts
if (!empty($posts_by_year)) {
foreach ($posts_by_year as $year => $posts) {
echo '<div class="year-block">';
echo '<strong class="year-title">' . $year . '</strong>';
echo '<ul class="posts-by-year">';
foreach ($posts as $post) {
echo '<li>';
echo '<strong class="post-title">' . $post->post_title . '</strong>';
echo '</li>';
}
echo '</ul>';
echo '</div>';
}
}
Of course change the html formating how ever you want
You can try this one:
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'artikel',
'meta_key' => 'year',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
Hope it will be work!

How to show post in it's specific category, not parent

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.

Display subcategories of custom categories - WordPress

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

Custom post type Wordpress query by category

I have the following query which outputs a list of categories for my custom post type called STORIES.
<?php
$taxonomy = 'story-category';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo '<div class="category-grid-box">
<div class="category-grid-content">' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a> </div>
</div> ';
}
?>
This outputs a list of links for my categories and works great.
My problem is, I don't know how to write the query on the next page which will list all the posts in that chosen category.
So my query lists the categories...
- Apples
- Oranges
- Bananas
If you click on Apples and go to that page, what query do I use to list all of the STORIES that have the category APPLES?
Any ideas? Can't get any solution to work.
I have the following query, but it lists ALL of the categories and ALL of the posts within them. How can I modify it to just show the posts for the page I am on?
<?php
$custom_terms = get_terms('story-category');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'stories',
'tax_query' => array(
array(
'taxonomy' => 'story-category',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo '<p>'.get_the_title().'</p>';
endwhile;
}
}
?>
you can create custom taxonomy template for custom post : LINK
Hope this help:
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'story-category',
'field' => 'slug',
'terms' => $term->slug,
),
),
);
$query = new WP_Query( $args );
Class Reference/WP Query

my code is always returning ALL products for the specified category ID

Based on a category ID, I am trying to retrieve the relevant products under that category. However, all category ID I tried seems to return ALL products I have in the database and not those who belong to the category ID, here is my relevant code:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'category' => 24
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<br />' . woocommerce_get_product_thumbnail().' '.the_title().'';
endwhile;
wp_reset_query();
Even if I ommit the category argument, the code still returning All products. How to fix that so I get only the relevant products for the specified category ID?
Thanks in advance
Based on Anand comments, I figured out the way to do so:
$term = get_term($_GET['catId'],'product_cat');// first parameter is the cat ID
$args = array( 'post_type' => 'product',
'posts_per_page' => -1,// unlimited posts
'product_cat' => $term->name,// Pass the category name
);
More info here: http://codex.wordpress.org/Function_Reference/get_term
$term = get_term( 24, 'product_cat' );
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'product_cat' => $term->name
);

Resources