WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to make a shortcode to list all custom posts under the category they are under. Here is my code and I am no expert of coding but i thought that this would work. If someone can tell me what I am doing wrong would be helpful. Any help is appreciated! Thanks in advance! :)


add_shortcode( 'categorylist', 'msc_category_post_list_shortcode' );

function msc_category_post_list_shortcode($atts) {

    $categories_args = array(
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'taxonomy'                 => 'cars_category'


    $categories = get_categories( $categories_args );

    foreach( $categories as $category ) :

        $postlist_args = array(
            'posts_per_page'   => -1,
            'offset'           => 0,
            'category'         => $category->cat_ID,
            'orderby'          => 'post_date',
            'order'            => 'DESC',
            'post_type'        => 'msccar',
            'post_status'      => 'publish' );

            $custom_posts = get_posts( $postlist_args );
            foreach ( $custom_posts as $post ) : setup_postdata( $post ); ?>
                <?php $posts .= '<li>'; ?>
                    <?php $posts .= '<a href="' . the_permalink() . '">' . the_title() . '</a>' ?>
                <?php $posts .= '</li>'; ?>

            <?php endforeach; 
            return $posts;

share|improve this question

You are not dealing with categories. You are dealing with a custom taxonomy.

$postlist_args = array(
  'posts_per_page'   => -1,
  'offset'           => 0,
  // 'category'      => $category->cat_ID, // not this
  'tax_query'        => array(
      'taxonomy' => 'cars_category',
      'field'    => 'id',
      'terms'    =>  $category->term_ID,
  'orderby'          => 'post_date',
  'order'            => 'DESC',
  'post_type'        => 'msccar',
  'post_status'      => 'publish'

That is untested, and to test it would require a bit of setup on my end-- creating the taxonomy, posts, etc.-- but that should be the idea.

I should point out that while this (or some variation) should work, it also has the potential to be very inefficient. You are going to run one database query for the categories plus one query per category returned. You can easily have 30, 40, 50, or more queries from that block of code alone.

It is my experience that it is nearly always more efficient to run as few queries as possible and shuffle the results in PHP.

See: http://wordpress.stackexchange.com/a/91263/21376

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.