I would like to query most of my pages and categories on the homepage. So far I am able to get the pages and category (posts), but I cannot order the list the way I want to. I want to be able to get a custom order like page, posts from a category, another page, and posts from another category.
I am using the following:
<?php
$args = array(
'cat' => $current,
'post_type' => array(
'page', 'post'),
'orderby' => 'ID' && array(2,4,1),
);
query_posts( $args );
while ( have_posts()) : the_post(); ?>
<?php // Include the page content template.
if ( is_page() ):
get_template_part( 'content', 'page' );
else:
get_template_part( 'content', 'post' );
endif;
?>
<?php endwhile;
?>
by the way I am using twentyfourteen and I don’t want to have multiple queries to that job unless it doesn’t put a burden on the web server. I want to save memory as much as I can.
2 Answers
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.