WordPress.org

Ready to get started?Download WordPress

Forums

Archive Loop displaying everything (5 posts)

  1. meevil
    Member
    Posted 1 year ago #

    Hey everyone,

    Having some trouble with my Archive loop in my custom theme. It's been working fine, but I've just noticed that instead of being context specific (displaying posts from a certain category, by a certain author etc.) it's now just showing everything that's ever been posted on the site.

    I've been trying to find the problem for hours now, but I'm completely stumped.

    Here's the code:

    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }
    
    $archiveArgs = array( 'posts_per_page' => 8, 'paged' => $paged, 'post_type' => array('post','review') );
    
    $archiveLoop = new WP_Query();
    
    $archiveLoop->query( $archiveArgs );
    
    while ( $archiveLoop->have_posts() ) : $archiveLoop->the_post(); ?>

    Any ideas?

    Thanks for taking the time have a look!

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    there is nothing in your custom query to tell the loop to only show posts of a particular archive;

    possibly have a look at:
    http://codex.wordpress.org/Function_Reference/query_posts#Preserving_Existing_Query_Parameters

    what theme are you working with, and where did you download it from?

  3. meevil
    Member
    Posted 1 year ago #

    Thanks for replying!

    I'm developing using the Underscores theme by Automattic - http://underscores.me/

    I've changed the original code to support my additional post type.

    This was the original archive loop -

    <?php /* Start the Loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>

    Could it simply be because I'm resetting the WordPress Query before the loop is being run?

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    possibly just try to change the original code to:

    <?php /* Start the Loop */ ?>
    <?php global $wp_query;
    $args = array_merge( $wp_query->query_vars, array( array('post','review') );
    query_posts( $args ); ?>
    <?php while ( have_posts() ) : the_post(); ?>

    (untested)

    or alternatively try and use 'pre_get_posts' action in functions.php of your theme:

    //add custom post type to archives//
    add_filter('pre_get_posts', 'add_archive_query_post_type');
    function add_archive_query_post_type($query) {
      if( is_archive() ) {
        $post_type = get_query_var('post_type');
    	if($post_type)
    	    $post_type = $post_type;
    	else
    	    $post_type = array('post','review');
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    (untested)

    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

  5. meevil
    Member
    Posted 1 year ago #

    First solution worked perfectly, thankyou very much!

    Thanks for also linking me to the pre_get_posts function. Looks interesting!

Topic Closed

This topic has been closed to new replies.

About this Topic