$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Order by Category and Post in WP custom Query|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Order by Category and Post in WP custom Query

matteradmin9PV0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 7

To do this you have first get all the category in ascending order by get_categories then you have to pass the cat_id in WP_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!

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far