WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Group custom post types by their type (6 posts)

  1. hydroplane
    Member
    Posted 4 years ago #

    I'm playing with the new custom post type feature. Does anyone know how can I group posts by their type?

    For example, I have a custom post type called 'Press' and I want to group them together so that when I go to mysite.com/press I'll have a list of all the 'Press' posts only.
    Is it possible?

    Also...it's not a big deal as I don't want to display those custom posts on the main page anyway, but are they supposed to behave like this?
    That is, when I publish a new custom type post it's there as a single entry but it won't appear on the main index alongside the other regular posts.

  2. MichaelH
    Member
    Posted 4 years ago #

    For example, I have a custom post type called 'Press' and I want to group them together so that when I go to mysite.com/press I'll have a list of all the 'Press' posts only.
    Is it possible?

    Use http://sample.com/?post_type=press

    Also...it's not a big deal as I don't want to display those custom posts on the main page anyway, but are they supposed to behave like this? That is, when I publish a new custom type post it's there as a single entry but it won't appear on the main index alongside the other regular posts.

    That's the way it is intended to work.

  3. hydroplane
    Member
    Posted 4 years ago #

    Thank you! I think I found another way, I'm posting it here for reference and hoping it will be useful to someone else. (Thanks to perishablepress.com for the loop template)

    Since I also wanted to have more control over the style and the appearance of those posts, I made a page called Press and applied this custom page template to it.
    In the example is just a very basic loop + the query_posts function (set to display my custom-post-type posts only) but you can start from it and style it any way you want.

    <?php
    
    /*
    Template Name: PostsOfPress
    */
    
    get_header(); ?>
    
    <?php
    //The Query
    query_posts('post_type=press');
    //The Loop
    if (have_posts()) : ?>
    
    <p>Display any code output from this region above the entire set of posts. This text is generated only if there are posts.</p>
    
    <?php while (have_posts()) : the_post(); ?>
    <! loop through posts and process each according to the code specified here >
    <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
    <?php endwhile; ?>
    
    <p>Stop the post loop and process any code included here only once. This text is generated only if there are posts. Any code included in this part of the document is processed only once.</p>
    
    <?php else : ?>
    
    <p>If you are seeing this message, there are no posts to process/display. Any code included in this part of the document is processed only once.</p>
    <?php endif; ?>
    
    <?php
    //Reset Query
    wp_reset_query();
    ?>
    
    <?php get_footer(); ?>
  4. MichaelH
    Member
    Posted 4 years ago #

    <?php
    //get all custom post types, if publicly_queryable,
    //display all published article titles for each of those types
    $args=array(
      '_builtin' => false,
    );
    $output = 'objects'; // names or objects
    $post_types=get_post_types($args,$output);
    foreach ($post_types  as $post_type ) {
      if (true == $post_type->publicly_queryable) {
        $type = $post_type->name;
        $args=array(
          'post_type' => $type,
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of ' . $post_type->label;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
          endwhile;
        }
        wp_reset_query();
      }
    }
    ?>
  5. Doodlebee
    Member
    Posted 4 years ago #

    Oh. My. God. I think I love you both.

  6. skeevis
    Member
    Posted 4 years ago #

    One issue if we're using query_posts or WP-Query, we lose all the URL variables, like the page/tag. How do we keep multiple instances of the loop intact?

Topic Closed

This topic has been closed to new replies.

About this Topic