Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9190547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:29:14+00:00 2026-06-17T20:29:14+00:00

I am using get_categories to show posts in a category in my frontpage, but

  • 0

I am using get_categories to show posts in a category in my frontpage, but I want to order categories as i defined,Like:

$cat_order = 5,2,4,1,3 //order by category ID
or
$cat_order = cat5,cat2,cat4,cat1,cat3 // order by cat name

below is my current code which order cat by name, ASC

<?php
//for each category, show all posts
$cat_args=array(
  'orderby' => 'name',
  'order' => 'ASC'
   );
$categories=get_categories($cat_args);
  foreach($categories as $category) {
    $args=array(
      'showposts' => -1,
      'category__in' => array($category->term_id),
      'caller_get_posts'=>1
    );
    $posts=get_posts($args);
      if ($posts) {
        echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        foreach($posts as $post) {
          setup_postdata($post); ?>
          <article >
            <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

            <?php the_content(); ?>
          </article>
          <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:29:15+00:00Added an answer on June 17, 2026 at 8:29 pm

    There’s no way to sort this within the query itself. You’ll have to sort it yourself once you get the results back. To sort an array by custom criteria, use the usort function. Here’s an example:

    $categories = get_categories();
    $cat_order  = array(5, 2, 4, 1, 3);
    
    usort($categories, function ($a, $b) {
        return ((int) array_search($a->term_id , $cat_order)) - ((int) array_search($b->term_id , $cat_order));
    });
    

    If you’re sure that all the categories are in your $cat_order array, you can forgo the integer casting.


    P.S. If you’re using PHP < 5.2, use a named function instead:

    function sort_categories ($a, $b) {
        return ((int) array_search($a->term_id , $cat_order)) - ((int) array_search($b->term_id , $cat_order));
    }
    
    usort($categories, 'sort_categories');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get a page using file_get_contents from a remote server, but I want to
I would like to display a list of all categories, using their hierarchy. I
I am getting the latest posts of all categories using the code below. I
I want to display posts for multiple categories. I have created a MULTISELECT drop
I have a Category resource, and am trying to link to Categories#show in its
In my page i have @using (Html.BeginForm(list, menu, FormMethod.Get)) { <div> Show categories: @Html.DropDownList(groupName,
I want to have a page that shows all posts, separated by category. The
I'm using Wordpress page templates to pull posts with certain categories into their own
Using get/set seems to be a common practice in Java (for various reasons), but
I am using get_headers() and I want to return the content length. In the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.