0

I noticed the latest posts widget in my footer displaying all posts. Anywhere else on my page, it displays only the amount of posts it should. I am using a custom theme and this is my archive.php:

<?php get_header(); ?>

<div id="maincol">

    <article id="page-<?php the_ID(); ?>" class="page">
        <header class="page-header">
            <h1 class="page-title">Post Type Archive Title</h1>
        </header>

        <?php $posts = query_posts($query_string . '&orderby=meta_value_num&meta_key=premium&posts_per_page=6'); ?>
        <?php if ( have_posts() ) : ?>

            <h3 class="toggle">Filter</h3>
            <div id="filterbox">
                 <?php echo do_shortcode('[ULWPQSF id=82 formtitle="0"]'); ?>
                 <small class="toggle-close">close</small>
            </div>

            <?php
                echo '<div class="objects">';
                echo '<ul class="list-objects clear">';
                while ( have_posts() ) { the_post();
                    get_template_part('content', 'my-post-type');
                }
                if(function_exists('wp_pagenavi')) :
                    wp_pagenavi();
                endif;
        endif; 
        echo '</ul>';
        echo '</div>';
        wp_reset_query();
        wp_reset_postdata();

        ?>

    </article>
 </div><!-- maincol-->

<aside class="sidebar" id="sidebar-main">
    <?php get_sidebar(); ?>
</aside>
<div class="clear"></div>

<?php get_footer(); ?>

Can you possibly spot the mistake?

  • 1
    The code you posted is a template file, not the widget code. Can you post the widget code? Anyway I can give a good advise: change the query_posts function for a new WP_Query instance. DO NOT USE query_post in a theme or plugin file. NEVER. – cybmeta Aug 29 '14 at 11:51
  • what is the shortcode ULWPQSF doing? – Michael Aug 29 '14 at 17:54
  • you should never change the main query on any type of archive page. You should use pre_get_posts to alter the main main query before it is executed instead. query_posts is a culprit to many problems, never use it at all. Take you time and read this post I've recently done – Pieter Goosen Aug 30 '14 at 5:17
  • ULWPQSF is a plugin shortcode for Ultimate WP Query Search Filter, it creates checkboxes for my categories to filter by. @cybmeta & Pieter Goosen: thank you, I didn't know using query_posts was that problematic, I used a pre_get_posts function, I'll post that solution later as an answer if you don't mind – okiedokey Sep 18 '14 at 9:12
  • That is why I always hammer on the point I've made. There are always beter more reliable methods to use than query_posts. I'm glad you solved your problem, and that I could be of help. BTW, +1 for just taking the time and posting the solution to your own question. :-) – Pieter Goosen Sep 18 '14 at 9:46
1

Thanks to the comments from @Pieter Goosen and @cybmeta I changed my way to order the posts on the archive page by making use of pre_get_posts

What I wanted my archive page to do is displaying posts with custom field value 1 and named "premium" (my method to create custom sticky posts) first and then all the "normal" posts.

I added the following lines to my functions.php file (Edit: added !is_admin() as suggested in Pieter's comment):

function custom_archive_query($query) {
    if ( is_post_type_archive('my-custom-post-type') &&  $query->is_main_query() && !is_admin() ) {
        $query->set( 'posts_per_page', 8 );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'meta_key', 'premium');
        $query->set( 'order', 'DESC' );
    }
}
add_action('pre_get_posts', 'custom_archive_query');

It's somewhat important to use $query->is_main_query() in the if-statement, because otherwise every other kind of query (e.g. WP menus or latest posts-widgets) won't display anything.

| improve this answer | |
  • It is very important to use is_main_query for the simple fact that pre_get_posts alters all queries, back end and front end, so you should direct it only at the main query. It is important to use !is_admin check as well to inhibit back end changes :-) – Pieter Goosen Sep 18 '14 at 9:41

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.