WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 2 years ago

#21436 closed defect (bug) (worksforme)

wp_query with custom post type and category not working

Reported by: voilamedia Owned by: voilamedia
Milestone: Priority: normal
Severity: normal Version: 3.4.1
Component: Query Keywords: reporter-feedback
Focuses: Cc:

Description

I have declared a custom post type in my theme called bluestar. There are some posts i dont want displayed on every page display those posts so i added support for categories in my register_post_type function using this solution http://bit.ly/bVazBo


'taxonomies' => array('category', 'post_tag')

Using wp_query i am able to output posts that are from that post_type and are not in that category using cat-id which works

 <?php  
  $wp_query = new WP_Query();
  $wp_query->query('post_type=bluestar&cat=-19&posts_per_page=-1'); ?>

The issue occurs when you try to do the reverse and only output posts from from the bluestar post_type that ARE in a specific category.

I have tried two methods both dont work

Method 1 using astraight query like before:

 <?php  $wp_query = new WP_Query();
	    $wp_query->query('post_type=bluestar&cat=19&posts_per_page=-1'); ?>

Method Two Array

<?php 
$wp_query = new WP_Query();
$wp_query->query(array( 'post_type' => 'bluestar', 'cat' => 19, 'posts_per_page' => -1));
?>

I have tried several variations but nothing seems to work so i believe its a bug. I have tested this in all modern browsers on a mac running mac osx mountain lion. I also have tried deactivating all plugins but it still persists. I am using the latest version of wp (3.4.1)

Please advise

Thanks
-Yosef

Change History (2)

comment:1 @SergeyBiryukov3 years ago

  • Keywords reporter-feedback added

Couldn't reproduce on a clean install with Twenty Twelve.

My steps:

  1. Created the post type:
    function bluestar_post_type() {
    	register_post_type( 'bluestar', array(
    		'label' => 'Bluestars',
    		'public' => true,
    		'taxonomies' => array( 'category', 'post_tag' )
    	) );
    }
    add_action( 'init', 'bluestar_post_type' );
    
  2. Added two posts (Bluestar 1 and Bluestar 2).
  3. Assigned them to different categories (with IDs 66 and 1).
  4. Added your query before the main loop:
    $wp_query = new WP_Query();
    $wp_query->query('post_type=bluestar&cat=-1&posts_per_page=-1'); ?>
    
  5. Bluestar 1 was displayed.
  6. Replaced cat=-1 with cat=1.
  7. Bluestar 2 was displayed.

comment:2 @SergeyBiryukov2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

Feel free to reopen with more information if there's still a problem.

Note: See TracTickets for help on using tickets.