WordPress.org

Ready to get started?Download WordPress

Forums

Query Post Type Category in WordPress (6 posts)

  1. ignitionmedia
    Member
    Posted 3 years ago #

    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.

  2. Bill Erickson
    Member
    Posted 3 years ago #

    • 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();
  3. ignitionmedia
    Member
    Posted 3 years ago #

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

  4. ignitionmedia
    Member
    Posted 3 years ago #

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

  5. Bill Erickson
    Member
    Posted 3 years ago #

    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.

  6. ignitionmedia
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic