Show category by latest post date - php

I am working in a project where I am using below code to limit number of posts in each category. But I can't figure out how to sort these categories by latest posts. My code is working fine but when I add argument in wp query array, it does not sort or it breaks. I want to order the category with most recent post (date).
Can anyone guide me?
<?php $terms=g et_terms( array( 'taxonomy'=>'category', 'hide_empty' => false, ) ); foreach($terms as $cat){ $cata_name = $cat->name; $term_id = $cat->term_id; ?>
<?php //echo '<h3>'.$catname[0]->cat_name.'</h3>'; ?>
<?php $catqueryy=n ew WP_Query ( 'cat='.$term_id. '&posts_per_page=5');$count=$ catqueryy->found_posts; ?>

You can add ORDER BY argument in wordpress as:
<?php
$args = array(
'post_type' => 'post', //your_post_type
'orderby' => 'date', //meta_value
'meta_query' => array(array('key' => 'cat','value'=>$term_id)),
'order' => 'DESC',
'posts_per_page' => 5,
);
$catqueryy = new WP_Query($args);
?>
You can explore the wordpress document: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

Related

WordPress: Category sort by Latest Post Date

I am working in a custom WP theme.I need to show each posts under individual categories, which is working fine.But, i want to sort the categories according to the published date of posts.
I found one answer, but its not working in my code and i cant figure how to use it in my code.So, i need anyone's expert help/input in below code.
Similar answer
**<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
'orderby' => 'date',
'order' => 'ASC',
) );
foreach($terms as $cat){
$cata_name = $cat->name;
$term_id = $cat->term_id;
$catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=10');$count = $catqueryy->found_posts;while($catqueryy->have_posts()) : $catqueryy->the_post();?>
<div class="list-group">
<p class="post_excerpt">
<?php echo ' '.__(get_the_title(),'rockon').''; ?>
</div>
<?php
endwhile;
?>
You can try this to show all categories by date
<?php $args = array(
'show_option_all' => '',
'orderby' => 'ID',
'order' => 'DESC',
); ?>
<?php wp_list_categories( $args ); ?>

Get post from CPT with same categoryname as the title

I'm working on a page where I want to show posts (custom post type) from a category which have the same categoryname as the title of the post.
I'm still learning, that's Why I hope you guys could help me out. :)
What I have:
CPT named 'shoes'
CPT categories: sneakers, boots, sportshoes
CPT named 'articles'
CPT categories: sneakers, boots, sportshoes
On the page ~/sneakers/ (from CPT 'shoes') I want to show articles from the category 'sneakers'.
This what I have till now:
global $post;
$cat_ID = array();
$categories = get_the_category();
foreach($categories as $category) {
array_push($cat_ID,$category->cat_ID);
}
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => '[THE_TITLE]',
'numberposts' => -1,
'category__in' => $cat_ID
);
$cat_posts = get_posts($args);
if ($cat_posts) {
foreach ($cat_posts as $cat_post) {
?>
<li><?php echo $cat_post->post_title; ?></li>
<?php
}
}
OK, based on the question and the following explanation:
We are in the single-shoes.php where the information for single shoe is shown.
We want to grab and display the information about articles custom post type, and this articles must be in specific category, which name is = post_title.
If this is the case you can do the following:
We want our posts to be from articles custom post type so we must put 'post_type' => 'articles'in the $args array.
We want returned post to be in category, which name = post_title, so we must put 'category_name' => get_the_title() into $args
The code is:
<?php
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'articles',
'posts_per_page' => -1,
'category_name' => get_the_title()
);
$articles = new WP_Query($args);
if ( $articles->have_posts() ) :
while( $articles->have_posts() ) : $articles->the_post(); ?>
<li><?php the_title();?></li>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
P.S. I'm still a little confused from the category name and still not sure that I understood correctly how this value is taken, however, if you can grab this value, the query is provided above.

WP Query Custom Taxonomy but exclude current term

I am setting up a wp_query to list all of the terms in a custom taxonomy. However, I'd like to ammend the code below to exclude the current taxonomy term.
For example, when on the archive.php page for 'term1', I'd like that to be excluded, meaning I am left with 'term2', 'term3' etc.
<?
$args = array(
'taxonomy' => 'topic',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => '-1'
);
?>
<? $tax_menu_items = get_categories( $args );
foreach ( $tax_menu_items as $tax_menu_item ):?>
<li>
<a href="<? echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>">
<? echo $tax_menu_item->name; ?>
</a>
</li>
<? endforeach; ?>
Try this, it does depend on where you are using it
// Get the category ID
$catID = get_query_var('topic');
// Now run the query excluding the current taxonomy
$args = array(
'exclude' => $catID,
'taxonomy' => 'topic',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => '-1'
);
Then the rest of your code above. For more on get_categories() take a look at https://codex.wordpress.org/Function_Reference/get_categories the exclude parameter is definitely what you need - it is just making sure you exclude the correct category ID.

Having trouble with wordpress custom taxonomy archive

I'm having a little trouble with a custom taxonomy template. I inherited a site that was developed by someone else and they use "Types" plugin to add some custom taxonomies.
Goal:
to have an archive template that shows only posts with a certain taxonomy term in it at example-domain.com/people/harrison-ford
Problem:
This code is bringing in posts that do not have the taxonomy selected.
Here's my full code:
<?php
$year = get_post_meta($post->ID, 'year', true);
$post_type = 'post';
$tax = 'people';
$tax_terms = get_terms( $tax );
if ($tax_terms) {
$args = array(
'post_type' => $post_type,
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'orderby' => 'date',
'order' => DESC
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<h2 class="wwNews"><?php echo $tax_term->name; ?> News</h2>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<-- display stuff -->
<?php endwhile; // end of loop ?>
<?php endif; // if have_posts()
wp_reset_query();
}
?>
What are you expecting here? "$tax" is going to to be 'people' =>, which is going to overwrite 'harrison-ford' to the value of $tax_term->slug.
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
Furthermore, I don't know of any custom argument called people, I'm pretty sure that you want tax_query:
'tax_query' => array(
'taxonomy' => 'people',
'terms' => array('harrison-ford', $tax_term->slug)
)
Which will give you the results of all people matching harrison-ford and the value of $tax_term->slug within the taxonomy of people

Exclude all sub categories from wp_query

I've been searching high and low for and answer to this, but I'm not actually sure it's possible!
I have a WP_Query that pulls posts from almost everything, however, I wish to exclude a specific category and/or all it's sub categories.
Searching around people are yet to find a solution for this.
Here's my query so far:
$args = array(
'post_type' => 'sell_media_item',
'cat' => -98,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php $loop = new WP_Query( $args ); ?>
I thought just excluding cat 98 would grab all the sub categories too, but apparently not.
I've tried using:
category__not_in, depth=0, parent=0 and even an adaptation of this, with no luck.
Any ideas?
[EDIT]
I'm using a custom taxonomy called Collections, so putting 'collection' => 'vip' into the query means it will only show this collection. I'm thinking if there's a way of reversing this so it excludes the collection instead?
As it's not possible to list all of the categories that will appear here as they will be changing all of the time.
[EDIT 2]
After the discussion in the comments below, here's the updated code.
$ex = array(
'taxonomy' => 'collection',
'child_of' => 98,
'hide_empty' => 0
);
$categories = get_categories($ex);
$categoriesToExclude = array();
foreach ($categories as $category) {
$categoriesToExclude[] = $category->cat_ID;
}
echo('<pre>'); var_dump($categories);
$args = array(
'post_type' => 'sell_media_item',
'category__not_in' => $categoriesToExclude,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php echo('<br /><pre>'); var_dump($args); ?>
<?php $loop = new WP_Query( $args ); ?>
I would get the list of all sub categories with get_categories() and then build a 'cat' exclusion array based on the results.
$args = array('parent' => 98);
$categories = get_categories($args);
$categoriesToExclude = array();
foreach ($categories as $category) {
$categoriesToExclude[] = $category->cat_ID;
}
$args = array(
'post_type' => 'sell_media_item',
'category__not_in' => $categoriesToExclude,
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
); ?>
<?php $loop = new WP_Query( $args ); ?>
This is just an example, you may have to modify it slightly to fit your needs.
So!
It appears I was trying to do the impossible. I couldn't get this script working for the life of me. So I tried a different angle. Instead of excluding a custom taxonomy and its terms, I decided to move all of my other terms into a parent term and just called that instead.
Here's the code if anyone's interested...
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'sell_media_item',
'taxonomy' => 'collection',
'term' => 'clubs',
'orderby' => 'desc',
'paged' => $paged,
'posts_per_page' => 20
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) : while ($loop->have_posts()) : $loop->the_post(); ?>
I wrote my own function in order to exclude subcategory posts from the loop, using tips from the above post and elsewhere.
In my theme archive.php file, above the loop, I list the subcategories (optional):
<?php
$current_cat = get_queried_object();
$args = array( 'parent'=>$current_cat->term_id, 'child_of' => $current_cat->term_id, );
$categories = get_categories( $args );
foreach($categories as $category) { ?>
<h2><?php echo $category->name ;?></h2>
<p> etc....</p>
<?php } ?>
In my functions.php file, I've added the following custom function using pre_get_posts:
add_action( 'pre_get_posts', 'main_query_without_subcategory_posts' );
function main_query_without_subcategory_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It's the main query for a front end page of your site.
if ( is_category() ) {
//Get the current category
$current_category = get_queried_object();
//get the id of the current category
$current_cat_id = $current_category->term_id;
//find the children of current category
$cat_args = array( 'parent'=>$current_category->term_id, 'child_of' => $current_category->term_id, );
$subcategories = get_categories( $cat_args );
//Get a list of subcategory ids, stick a minus sign in front
$subcat_id = array();
foreach($subcategories as $subcategory) {
$subcat_id[] = " -". $subcategory->term_id;
}
//join them together as a string with a comma seperator
$excludesubcatlist = join(',', $subcat_id);
//If you have multiple parameters, use $query->set multiple times
$query->set( 'posts_per_page', '10' );
$query->set( 'cat', ''.$current_cat_id.','.$excludesubcatlist.'' );
}
}
}
Then in the archive.php, below the subcategories, I've added the regular WordPress loop which is now being modified by the above function:
<?php while (have_posts() ) : the_post(); ?>
<h2><?php the_title();?></h2>
<p> etc....</p>
<?php endwhile;?>
Though the WordPress codex says that using "category__in" will exclude posts from subcategories, that didn't work for me and subcategory posts were still showing.
https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
https://developer.wordpress.org/reference/hooks/pre_get_posts/

Categories