4

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.

1
  • I think your orderby would be 'orderby' => array( 'ID' => array(2,4,1), 'ID' => 'ASC' )
    – admcfajn
    Dec 31, 2017 at 17:51

2 Answers 2

0

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.

2
  • Sorry, but this is really less of an answer. You are insinuating that I did not do any research or I am better off hiring someone. Please tell me something I don't know. Jul 22, 2014 at 10:35
  • I could not post this as a comment. I'm not saying you did not do any research, I gave you some ideas that you can search for. This is a really big task, which I'm not prepared to code. You have to remember one thing, everybody on this site, even the moderators are just members of this site, exactly like you. We spend our own time volunteraly here out of love for wordpress and the satisfaction trying to help others. I'm stating the facts here, so don't get uptide or rude. Jul 22, 2014 at 10:52
0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.