• Hi there,
    I have a wordpress website which I need the code/query which all allow me to display all other posts that belong to the category of the current post type that is being viewed.

    Eg;
    Custom Post Type: Accommodation
    Taxonomy: apartmentcat
    Category: Chevron Renaissance

    So If I were on a page for Apartment 101 from the Chevron Renaissance, I would also be able to see a list of the other apartments in the Chevron Renaissance.

    As there are new Categories added all the time, therefore I cannot hard code this query such as;
    <?php query_posts(‘post_type=accommodation&apartmentcat=chevron-renaissance&showposts=20&orderby=menu_order’); ?>

    This works, however it would then require separate templates per category which is not what I want. I need a code that will allow one template to work from all Categories and show the posts related to the current posts category that is being viewed.

    I have posted 2 other posts on this and had no reply and am desperate for the answer. Any help at all is appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
    • First I load all the terms associated with this post and in the taxonomy ‘apartmentcat’ into the variable $terms.
    • I then cycle through them, assigning the slugs to an array. I’m doing this so if more than one terms are associated with the post it doesn’t mess up your query.
    • Then I create a new WordPress query to pull in posts of the accommodation post type, the current term for the taxonomy apartmentcat, 20 posts, in menu order.
    • If there’s posts that match this query I make an unordered list with their titles linked to the permalink.
    <?php
    global $post;
    $terms = get_the_terms($post->ID, 'apartmentcat');
    $slugs = array();
    foreach ( $terms as $term )
    $slugs[] = $term->slug;
    $other_posts = new WP_Query('post_type=accommodation&apartmentcat='.$slugs[0].'&showposts=20&orderby=menu_order');
    if ($other_posts->have_posts()):
    echo '<ul>';
    while ($other_posts->have_posts()): $other_posts->the_post();
    echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    endwhile;
    echo '</ul>';
    endif;
    wp_reset_query();
    Thread Starter ignitionmedia

    (@ignitionmedia)

    Oh you legend… let me see how I go with that!

    Thread Starter ignitionmedia

    (@ignitionmedia)

    OMG Billerickson you are my hero!!!!! I can’t thank you enough! works like a charm!

    Btw, dont suppose you do freelance work? Im sure you could come in very handy for my clients WP sites.. i know only the very basics and would love to have a WP genius on hand for situations like this.

    thanks again 🙂

    Yep, you can check me out at http://www.billerickson.net

    Sorry about the error. Although only one error in untested code like that is pretty good 🙂 I’m fixing it now.

    Thread Starter ignitionmedia

    (@ignitionmedia)

    thank you once again. Im sure I will be in touch in my next time of need 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Query Post Type Category in WordPress’ is closed to new replies.