Get custom post type by category in a page template

I've created a custom post type with categories and sub categories, what I need to do is list out the post titles and images for a given sub-category or category in a page template.

I've got as far as getting the all the listed items in the custom post type, but I'm unsure how to go further... any help appreciated.

<?php 
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="infoflowing-style">';
the_content();
echo '</div>';
endwhile;
?>

Comments


  • lonely

    This should work:

    $cat_id = get_cat_ID('My Category'); //the categories name
    $args=array(
      'cat' => $cat_id,
      'post_type' => 'portfolio',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $new = new WP_Query($args);
    
  • Diamonds

    I think it's possible the below code did not work because it uses a deprecated parameter (I think caller_get_posts was deprecated in 3.1)

    Think the below should sort things out:

    $loop = new WP_Query( array( 
        'post_type' => 'portfolio', 
        'cat' => 5, // Whatever the category ID is for your aerial category
        'posts_per_page' => 10,
        'orderby' => 'date', // Purely optional - just for some ordering
        'order' => 'DESC' // Ditto
    ) );
    
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    

    Couple of things to consider as well (sorry if this crosses into 'obvious' territory!):

    1) is your custom post type registered to use the built in categories or is it a custom taxonomy that it's using? If the former then the above should work, if the latter then you would need to use 'your-taxonomy-name'=>'your-taxonomy-term' in place of the cat=>5 parameter

    http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

    2) Do you have any other loops running on the page? If so they will need

    <?php wp_reset_query(); ?>
    

    after them in order for subsequent loops to work properly

    http://codex.wordpress.org/Function_Reference/wp_reset_query

Add Comment