WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] query_posts in loop.php overrides the archive page (16 posts)

  1. greatdanemaniac
    Member
    Posted 4 years ago #

    I'm using this line of code to display my custom post types, and it works like a charm.

    <?php query_posts( array( 'post_type' => array('essay', 'tutorial', 'post', 'gamereview') ) ); ?>

    However, I recently noticed that my archive pages are not working because of this. When I remove the code, the archive page functions normally again.

    Can somebody please show me a better way to make it work?
    Thanks!

  2. alexpts
    Member
    Posted 4 years ago #

    Hi :)

    To add these, open your theme’s functions.php file and paste this PHP code into it:

    add_filter( 'pre_get_posts', 'my_get_posts' );

    function my_get_posts( $query ) {

    $query->set( 'post_type', array( 'essay', 'tutorial', 'post', 'gamereview' ) );
    return $query;
    }

  3. greatdanemaniac
    Member
    Posted 4 years ago #

    I've tried that before, but it doesn't work anymore. I don't know why it doesn't work, since it did with the alpha and beta versions of 3.0. With the final version, it doesn't seem to work. Strange, I know...

    Well, it couldn't hurt to try it once more. Thanks for your reply!

  4. greatdanemaniac
    Member
    Posted 4 years ago #

    Hey, it did work, although the code totally screwed up my site, so I had to delete it.

    My menu went missing, posts turned into pages and vice versa... Don't really know why though.

  5. alexpts
    Member
    Posted 4 years ago #

    I'm using the stable Russian version of WordPress 3 and it works for me. I do not know why not work for you...

    My code:

    function post_type_albums() {
    register_post_type(
    'albums',
    array('label' => __('Albums'),
    'public' => true,
    'show_ui' => true,
    'supports' => array(
    'title',
    'editor',
    'custom-fields',
    'post-thumbnails',
    'excerpts',
    'trackbacks',
    'comments')
    )
    );

    register_taxonomy( 'genres', 'albums', array( 'hierarchical' => true, 'label' => __('Genres') ) );

    register_taxonomy( 'performer', 'albums',
    array(
    'hierarchical' => false,
    'label' => __('Performer'),
    'query_var' => 'performer',
    'rewrite' => array('slug' => 'performer' )
    )
    );
    }

    add_action('init', 'post_type_albums');
    add_filter( 'pre_get_posts', 'my_get_posts' );

    function my_get_posts( $query ) {
    $query->set( 'post_type', array( 'albums', 'post') );
    return $query;
    }

  6. greatdanemaniac
    Member
    Posted 4 years ago #

    Ok, well I use a plugin (GD CPT Tools) to create my custom post types. That shouldn't even matter though...

    Well, I can manage without archives. Don't think people usually visits them anyway...

  7. alexpts
    Member
    Posted 4 years ago #

    try
    http://wordpress.org/extend/plugins/custom-post-type-ui/

    Don't think people usually visits them anyway...

    I think so too

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    The reason the filter/action messes with the all the content is because there's no conditional statements in the filter, so it intercepts all kind of queries, including some in the admin side of the site.

    Kind of same deal with your query_posts declaration, when you define the query parameters you basically overwrite any existing parameters with that name, and kill/remove any you havn't redeclared in the query.

    So let's take a step back.

    This code.

    <?php query_posts( array( 'post_type' => array('essay', 'tutorial', 'post', 'gamereview') ) ); ?>

    When should it run?

    On the home page, or a particular page, or in a certain case? If you could clarify i'll help you fix the query.. ;)

  9. greatdanemaniac
    Member
    Posted 4 years ago #

    Thanks t31os_!

    I'm having this code in my loop.php file, since I'd like all custom post types and my original posts to be displayed within my blog/homepage.

  10. Mark / t31os
    Moderator
    Posted 4 years ago #

    Ok great, but under which condition(s) should those parameters be passed into the query?

  11. davoscript
    Member
    Posted 4 years ago #

    Hi greatdanemaniac, as t3los_ suggest, you should determinate where you want to show that. I see you say you want your "custom post types" and "original posts" in the "HOMEPAGE".. so try something like this:

    <?php if ( is_home() ) : ?>
    
    <?php query_posts( array( 'post_type' => array('essay', 'tutorial', 'post', 'gamereview') ) ); ?>
    
    <?php endif; ?>
    
    <?php THE LOOP... ?>

    This way, the query_posts function will only affect the loop when in the front page.

    Best Regards,
    David

  12. Mark / t31os
    Moderator
    Posted 4 years ago #

    Or alternatively do the conditional statement in the filter(filter or query_posts, the end result will be the same).

    If you need any help writing a different conditional statement, we're here to help... :)

  13. greatdanemaniac
    Member
    Posted 4 years ago #

    Oh, thanks!
    I'll sure try it, Davoscript. Thanks!

    I'll get back to you with the results.

  14. greatdanemaniac
    Member
    Posted 4 years ago #

    Yes! That worked like a charm!

    Thank you, davoscript!

  15. davoscript
    Member
    Posted 4 years ago #

    Glad to read that!

    Regards!

  16. syrus69
    Member
    Posted 4 years ago #

    nice works like a charm,
    but if we would like to display them on category pages would we change the code to?
    sorry a newb at this

Topic Closed

This topic has been closed to new replies.

About this Topic