NB: I don't have enough reputation to ask the question directly on the post mentioned.
I've used some code from 1: Get all categories and posts in those categories
It lists all the Categories with the Posts listed inside those Categories, but I'm having trouble setting the orderby ...
Specifically the Revisit answer here
<?php
$args = array(
'posts_per_page' => -1
);
$query = new WP_Query($args);
$q = array();
while ( $query->have_posts() ) {
$query->the_post();
$a = '<a href="'. get_permalink() .'">' . get_the_title() .'</a>';
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
$b = '<h2><a href="' . get_category_link( $category ) . '">' . $category->name . '</a></h2>';
}
$q[$b][] = $a; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
foreach ($q as $key=>$values) {
echo $key;
echo '<ul>';
foreach ($values as $value){
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
?>
What i'm trying to do is add ordering by Category Title, then within that category orderby Post Title.
I can add it in the intial $args on line 1
$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
);
This sorts the categories in order of the title of the posts contained in that category.
I'm trying to get the Categories in ASC order, then the posts within that category in ASC order but I can't figure out how to order them both.
Be very grateful for any advice
NB: I don't have enough reputation to ask the question directly on the post mentioned.
I've used some code from 1: Get all categories and posts in those categories
It lists all the Categories with the Posts listed inside those Categories, but I'm having trouble setting the orderby ...
Specifically the Revisit answer here
<?php
$args = array(
'posts_per_page' => -1
);
$query = new WP_Query($args);
$q = array();
while ( $query->have_posts() ) {
$query->the_post();
$a = '<a href="'. get_permalink() .'">' . get_the_title() .'</a>';
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
$b = '<h2><a href="' . get_category_link( $category ) . '">' . $category->name . '</a></h2>';
}
$q[$b][] = $a; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
foreach ($q as $key=>$values) {
echo $key;
echo '<ul>';
foreach ($values as $value){
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
?>
What i'm trying to do is add ordering by Category Title, then within that category orderby Post Title.
I can add it in the intial $args on line 1
$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
);
This sorts the categories in order of the title of the posts contained in that category.
I'm trying to get the Categories in ASC order, then the posts within that category in ASC order but I can't figure out how to order them both.
Be very grateful for any advice
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Feb 5, 2017 at 5:22 webechowebecho 531 gold badge1 silver badge4 bronze badges1 Answer
Reset to default 7To do this you have first get all the category in ascending order by
get_categories
then you have to pass the cat_id inWP_Query
to get the post related with that category.
$args_cat = [
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
];
$categories = get_categories($args_cat);
//print_r($categories);
if (!empty($categories)):
foreach ($categories as $category):
$args = [
'post_type' => 'post',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'cat' => $category->term_id
];
$query = new WP_Query($args);
while ($query->have_posts()) : $query->the_post();
//You code
the_title();
//...
endwhile;
wp_reset_postdata(); // reset the query
endforeach;
endif;
Hope this helps!