How do I order pages and categories by ID or name in the same query?

First of all, never use query_posts, ever. My emphasis. It is outright not meant to be used at all, and should be removed in future wordpress versions. Overall performance wise I would say it is even worse than a custom query. You should really be using WP_Query for the task at hand

Note: This function isn’t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

You cannot unfortunately do this in one query as you cannot sort within the loop, and there is also no native orderby function in wordpress to accomplish this. BTW, your use of orderby is incorrect. Have a look at the orderby parameters in WP_Query

You’ll need to run at least two, maybe even more custom queries, to achieve what you want. Once you have the queries, they must be merged, and this is where your real headache start.

If you are going to need pagination, this is going to get quite complicated unfortunately.

I don’t think this is really a question to be answered here, as it would involve a lot coding, etc. I don’t like answering a question in this fashion, but it would really be better to hire a professional to help you with this. Also, you have to go and do your research yourself and test some code to see what works and what doesn’t.

Leave a Comment