WordPress.org

Ready to get started?Download WordPress

Forums

WP_Query with custom post_type and global category (10 posts)

  1. pattydelicious
    Member
    Posted 2 years ago #

    I'm trying to do a wp_query that filters 1) my custom post types and 2) the general category "Featured" which can be used by all post types.

    Right now wp_query seems to be ignoring my post_type argument, but limits the category_name perfectly. Help?

    <?php
      	$loop = new WP_Query( array(
        		'showposts' => 5,
        		'post_type' => array('videos', 'image', 'audio'),
        		'category_name' => Featured,
        		)
        		);
      	while ( $loop->have_posts() ) : $loop->the_post(); ?>
       	<div>
          		<h2><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
          		<div class="entry-summary">
            		<?php the_excerpt(); ?>
        		</div>
        	</div>
      <?php endwhile; ?>
  2. Jackson
    Member
    Posted 2 years ago #

    Is this verbatim?
    'category_name' => Featured,
    If so, try:
    'category_name' => 'Featured',

  3. pattydelicious
    Member
    Posted 2 years ago #

    I fixed that, but it doesn't help. The code still filters only by the category "Featured" but not by the post types.

  4. Mitchell
    Member
    Posted 2 years ago #

    1. you should be using posts_per_page and not showposts (see Codex: http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters)

    Just to confirm, because the post type naming is throwing me off, you are in fact working with "Post types" and not "Post formats"?

  5. pattydelicious
    Member
    Posted 2 years ago #

    Fixed posts_per_page, and correct they are Post Types and not formats.

  6. Digital Raindrops
    Member
    Posted 2 years ago #

    Case sensitive?

    category_name (string) - use category slug (NOT name).

    Use the Featured slug value which will be lowercase

    'category_name' => featured,

    For a two word category name like 'Latest Posts'

    'category_name' => latest-posts,

    Read it again, see you have fixed that one, the code looks fine maybe just construct it differently use an $args array?

    $args = array(
        	'posts_per_page' => 5,
        	'post_type' => array('videos','image','audio'),
        	'category_name' => 'featured',
        );
    $loop = new WP_Query( $args );

    HTH

    David

  7. Mitchell
    Member
    Posted 2 years ago #

    what does $loop->query output for you?

  8. pattydelicious
    Member
    Posted 2 years ago #

    Still having the problem. Currently, the loop outputs every post type within the category 'featured'

    What I want is for only certain post type (videos, image, audio) posts that are in the category 'featured' to appear.

  9. Jackson
    Member
    Posted 2 years ago #

    How are you registering your CPT's?

    You might try using tax_query:
    http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

    Just a thought...

  10. Digital Raindrops
    Member
    Posted 2 years ago #

    Try creating an empty query first, this adapted structure is what I use for a template page?

    <?php
    $args = array(
        'post_type' => array('videos','image','audio'),
        'category_name' => 'featured',
        'showposts'=> 13,
        'paged' => $paged,
    );
    
    $wp_query= null;
    $wp_query = new WP_Query();
    $wp_query->query($args);
    ?>
    
    <?php if ( $wp_query->have_posts() ) : ?>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    
    <?php endwhile; ?>
    <?php endif; ?>

    EDIT:
    We had another topic last week for testimonials, and I know this code worked.

    I created a twenty eleven template page that populated with the CPT testimonials.
    http://pastebin.com/t1b1VMds

    ANOTHER EDIT
    Just noticed that the original code is missing the have_posts(), so you might be getting a false record set?

    Missing:
    <?php if ( $loop->have_posts() ) : ?>

    HTH

    David

Topic Closed

This topic has been closed to new replies.

About this Topic