admin管理员组文章数量:1130349
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_categoriesthen you have to pass the cat_id inWP_Queryto 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!
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_categoriesthen you have to pass the cat_id inWP_Queryto 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!
本文标签: Order by Category and Post in WP custom Query
版权声明:本文标题:Order by Category and Post in WP custom Query 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749251778a2339810.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论