WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying all categories and posts of a certain custom post type (2 posts)

  1. carbonmadex
    Member
    Posted 3 years ago #

    Hello guys,

    I’m new to custom post types and don’t know how to display all of the contents of a certain custom post type in a page.

    For example if I have a custom post type named “portfolio”, and if there are categories under it named “x”, “y”, “z”. I want to have a page like this:

    portfolio:

    x:

    * x-post1
    * x-post2

    y:

    * y-post1
    * y-post2

    z:

    * z-post1
    * z-post2

    Trying to make it so whatever “categories” get created will show.
    I would appreciate any help. Thanks in advance.

  2. MichaelH
    Member
    Posted 3 years ago #

    This should do it

    List all posts sorted by tag name or category name or taxonomy custom taxonomy
    Within each category posts sorted ascending by post title.

    <?php
    //get all terms (e.g. categories or post tags), then display all posts in each term
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
      'include' => '1,2,3',
      'orderby' => 'name',
      'order' => 'ASC',
      'child_of' => 3
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'order' => 'ASC',
          'orderby' => 'title',
          'post_type' => 'portfolio',
          '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 Posts in '.$taxonomy .' '.$term->name;
          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();  // Restore global post data stomped by the_post().
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic