• Resolved Aimee

    (@greatdanemaniac)


    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!

Viewing 15 replies - 1 through 15 (of 15 total)
  • 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;
    }

    Thread Starter Aimee

    (@greatdanemaniac)

    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!

    Thread Starter Aimee

    (@greatdanemaniac)

    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.

    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;
    }

    Thread Starter Aimee

    (@greatdanemaniac)

    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…

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

    Don’t think people usually visits them anyway…

    I think so too

    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.. πŸ˜‰

    Thread Starter Aimee

    (@greatdanemaniac)

    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.

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

    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

    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… πŸ™‚

    Thread Starter Aimee

    (@greatdanemaniac)

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

    I’ll get back to you with the results.

    Thread Starter Aimee

    (@greatdanemaniac)

    Yes! That worked like a charm!

    Thank you, davoscript!

    Glad to read that!

    Regards!

    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

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘query_posts in loop.php overrides the archive page’ is closed to new replies.