• Resolved raytri

    (@raytri)


    I have a site with several custom post types.

    I’ve set all my post types to use the same set of categories, with the following line in the registration $args:

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

    That part works fine.

    But when I try to do a query based on post_type and category, I get all results for that category, regardless of post_type.

    For instance, here’s a shortcode that should only display results from the custom post type “emc-album’:

    /* Photo albums */
    function emc_photo_albums($atts, $content = null) {
    	    $a = shortcode_atts( array(
            'number' => '',
            'albumcat' => ''
        ), $atts );
    
    	$albumCode = '<h2>Photo albums | <a class="see-all" href="'.get_home_url().'/emc-album/">See all photos</a></h2><ul id="album_covers" class="list-unstyled">';
    
    	$album_args = 'post_type=emc-album&number='.$a["number"].'&category_name='.$a["albumcat"].'&orderby=date';
    
    	$album_query = new WP_Query($album_args);
    
    	if ($album_query->have_posts()) : while ($album_query->have_posts()) : $album_query->the_post();
    
        		$albumCode .= '<li class="album-grid"><a class="clickable-div" href="'.get_permalink().'" title="'.get_the_title().'">&nbsp;</a>'.get_the_post_thumbnail($post->ID,"album-thumb").'<div class="album-title">'.get_the_title().'</div></li>';
    
        	endwhile;
        endif;
    
    	$albumCode .= '<li class="clearfix"></li></ul>';
    	wp_reset_postdata();
    	return $albumCode;
    	die();
    }
    add_shortcode('emc-photo-albums','emc_photo_albums');

    The shortcode itself looks like this:

    [emc-photo-albums albumcat="membership"]

    If I don’t set a value for albumcat, I get what I expect: all posts of the “emc-album” type.

    But if I set a value for albumcat, I get all posts with that category, regardless of post-type.

    Any help would be appreciated!

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP_Query with custom post type and default categories’ is closed to new replies.