• I’m trying to get posts according to their category, but for some odd reason it returns all posts of the custom type:

    //here I get each  relevant category then proceed to loop trough them:
    
    foreach ($categories as $term ) {
        $idObj = get_category_by_slug($term->slug);
        $id = $term->term_id;
    
        $args = array( 'numberposts' => 5, 'post_type'  => 'custom', 'custom_category' => $term->slug , 'post_status' => 'publish');
        $myposts = get_posts( $args );
    
    }

Viewing 6 replies - 1 through 6 (of 6 total)
  • Try using category instead of custom_category:

    $args = array(
        'post_type' => 'custom',
        'category' => $term->slug
    );

    Template Tags/get posts

    P.S. post_status and numberposts are, by default, set to publish and 5 respectively, so I took them out of my example.

    Thread Starter d0100

    (@d0100)

    On the Codex:

    Custom Post Type Category (Taxonomy)
    
    If specifying a category from a custom post type then instead of using 'category' you would use '{your_custom_post_type}_category'. For instance, if you had a custom post type called "dogfood" and wanted to only show posts from the category "brand" you would use the below code.

    If I only use ‘category’ => $term->slug , it returns nothing.

    Is $categories an array of term objects populated before the foreach statement? If $term is already a term object, what’s the purpose of $idObj? Check to see that $id is being set to a valid term ID; being empty would result in get_posts() returning the last five posts regardless of category. Perhaps you wanted to use $idObj to get a full term object to then find the ID of the specific category?

    $idObj = get_category_by_slug( $term->slug );
    $id = $idObj->term_id;
    Thread Starter d0100

    (@d0100)

    $categories is:

    $categories = get_terms( 'my_taxonomy', array(
                    'orderby'    => 'count',
                    'hide_empty' => 0
                 ) );

    So $categories is an array of term objects from a custom taxonomy rather than actual WordPress categories.

    Looking at the core code, get_posts() itself doesn’t do anything other than chunk the arguments given into a new WP_Query. With a custom post type and a custom taxonomy, this would be the way to go:

    $args = array(
        'post_type' => 'custom',
        'tax_query' => array(
            'taxonomy' => 'my_taxonomy',
            'field' => 'id',
            'term' => $term->term_id
        )
    );

    Class Reference/WP Query

    As for querying for posts with actual WordPress categories, {your_custom_post_type}_category seems to be either old or just wrong. Your current query returns every post because it doesn’t know what to do with that parameter and is thus ignoring it. It returns nothing when you use category because you’re assigning terms from a custom taxonomy rather than categories.

    Thank you Big Bagel you solved my problem here.

    $cat = get_term_by('name', single_cat_title('',false), 'category');
    	$idObj = get_category_by_slug( $cat->slug );
    	$id = $idObj->term_id;
    
    	$args = array(
    	   'post_type' => 'event',
    	   'category' => $id
    	);
    	$posts_array = get_posts( $args );
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘get_posts() not returning only selected category posts’ is closed to new replies.