admin管理员组文章数量:1122846
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.
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.
Share Improve this question asked Jul 22, 2014 at 4:24 cmsdeployedcmsdeployed 3893 gold badges7 silver badges19 bronze badges 1 |2 Answers
Reset to default 0First 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.
Thanks for the previous answer. There mights be a misunderstanding. I didn't think your comment was rude @Pieter Goosen and mine wasn't with intention either. Anyway. I was able to come up with the bellow queries. I am querying categories inside pages. Don't know if it truly the proper way, but it works for me. I still have to do more digging to cleanup and see if there is any adverse effect. But so far I am getting exactly what I needed and with additional tweaks, I will be able to style my pages and categories like I need to.
<?php
$pages = get_pages(array (
'post_type' => 'page',
'sort_column' => 'menu_order',
));
foreach ($pages as $page) {
$apage = $page->post_name; ?>
<?php if ( $apage =='about') { ?>
<article id="<?php echo $page->post_name; ?>">
<header class="entry-header">
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { the_post_thumbnail(); } ?>
<h2 class="entry-title"><?php echo $page->post_title; ?></h2>
</header>
<div class="entry-summary"><?php echo apply_filters('the_content', $page->post_content); ?></div>
</article>
<!-- ## Now Hold the pages query to introduce the categories ## -->
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
/* if ( in_category('reviews') ) : */
echo '<header class="entry-header">'.$cat->name.'</header>';
// create a custom wordpress query
$args = array('cat=$cat_id&posts_per_page=5');
$the_query = new WP_Query( $args );
// start the wordpress loop!
if ($the_query->have_posts()) :
while ($the_query->have_posts()) :
$the_query->the_post();
// create our link now that the post is setup ?>
<?php if ( (in_category('reviews')) /*& ( category_slug == 'reviews' )*/ ) : ?>
<article id="cat-reviews">
<header class="entry-header">
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) { the_post_thumbnail(); } ?>
<h2 class="entry-title"><?php the_title(); ?></h2>
</header>
<div class="entry-summary"><?php the_excerpt(); ?></div>
</article><!-- #cat-reviews -->
<!-- ## More if categories as needed ## -->
<?php endif; ?>
<?php
endwhile;
endif; // done our categories loop.
/* endif; // done specific category */
} // done the foreach statement
/*
* getting back to our page query
*/
} elseif ( $apage =='contact') { ?>
<article id="<?php echo $page->post_name; ?>">
<header class="entry-header">
<h2 class="entry-title"><?php echo $page->post_title; ?></h2>
</header>
<div class="entry-summary"><?php echo apply_filters('the_content', $page->post_content); ?></div>
</article>
<!-- ## More if pages as needed ## -->
<?php } // end if
} // end foreach ?>
Yes, I will need to reset the query!
本文标签: How do I order pages and categories by ID or name in the same query
版权声明:本文标题:How do I order pages and categories by ID or name in the same query? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736284625a1927289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'orderby' => array( 'ID' => array(2,4,1), 'ID' => 'ASC' )
– admcfajn Commented Dec 31, 2017 at 17:51