WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Get category from a custom post

[Resolved] Get category from a custom post

  • Hi, I was wondering how can I get categories from a post type, since the regular query_post won’t work.
    I tried: query_posts( 'post_type=product&posts_per_page=3&product_category=41' );

    And nothing. It won’t return me the post within that category. How does this work?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Sorry, my query was
    query_posts( 'post_type=product&posts_per_page=3&cat=41' );

    The one I posted first was an attemp to make it work.

    Not sure why query posts acts weird sometimes, try:

    query_posts( array (
    'category_name' => 'my-category-slug',
    'posts_per_page' => 3,
    'post_type' => 'product'
    ) );

    Nope. Not a thing.

    I looked in the wordpress admin page and got this in the URL for the category:
    wordpress/wp-admin/edit-tags.php?action=edit&taxonomy=product_category&tag_ID=41&post_type=product

    So I tried adding the taxonomy as well. But nothing.

    This category that I want to use ONLY belong to the ‘product’ post_type, not to the regular posts!

    Can’t you use the category.php file then?

    No, this is part of the header and shows only posts of this category.
    It’s a section for “important” products.

    For secondary loops, use get_posts instead of query_posts:
    http://codex.wordpress.org/Template_Tags/get_posts

    cool! I’ll be looking at it!

    Wohooo!!thanks, this was most helpfull!

    $args = array(
    	'numberposts' => 4,
    	'orderby' => 'post_date',
    	'post_type' => 'product', //my type of post
    	'taxonomy' => 'product_category', //the regular categories for post_types
    	'product_category' => 'destacado', //The name of the regular category
      	'post_status' => 'publish'
    );
    $destacados = get_posts( $args );
    foreach( $destacados as $post ) :
    //... My post information here
    endforeach;

    With this I solved my problem! 🙂

    Hope this can help someone!

    I’m closing this post!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] Get category from a custom post’ is closed to new replies.
Skip to toolbar