WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get category from a custom post (10 posts)

  1. RandallFlagg
    Member
    Posted 1 year ago #

    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?

  2. RandallFlagg
    Member
    Posted 1 year ago #

    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.

  3. Peter_L
    Member
    Posted 1 year ago #

    Not sure why query posts acts weird sometimes, try:

    query_posts( array (
    'category_name' => 'my-category-slug',
    'posts_per_page' => 3,
    'post_type' => 'product'
    ) );
  4. RandallFlagg
    Member
    Posted 1 year ago #

    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!

  5. Peter_L
    Member
    Posted 1 year ago #

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

  6. RandallFlagg
    Member
    Posted 1 year ago #

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

  7. Peter_L
    Member
    Posted 1 year ago #

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

  8. RandallFlagg
    Member
    Posted 1 year ago #

    cool! I'll be looking at it!

  9. RandallFlagg
    Member
    Posted 1 year ago #

    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! :)

  10. RandallFlagg
    Member
    Posted 1 year ago #

    Hope this can help someone!

    I'm closing this post!

Topic Closed

This topic has been closed to new replies.

About this Topic