• Resolved DesignLoud

    (@designloud)


    I have been pecking away at this for awhile now and feel like it should not be this difficult unless I am missing something quite obvious.

    I have a post type of “our_properties” and a registered taxonomy of “properties_community”. When someone clicks on Communities they are presented with a page that lists all the communities which works fine. However when a user clicks on a community they are brought to a page where it shows ALL posts from ALL communities. Instead I would like it to inherit the behavior as if someone were to click on a Category archive, so it would only show the posts or “properties” associated with that community.

    I am using 3 files. One is where I register the post type and taxonomy, I have another template that displays the single post type and an archive template that displays the communities and posts associated with each.

    Here is how I have registered my tax in that file:

    //taxonomies
    add_action( 'init', 'create_my_taxonomies', 0 );
    
    function create_my_taxonomies() {
    	register_taxonomy(
    		'properties_community',
    		'our_properties',
    		array(
    			'labels' => array(
    				'name' => 'Property Community',
    				'add_new_item' => 'Add New Community',
    				'new_item_name' => "New Community"
    			),
    			'show_ui' => true,
    			'show_tagcloud' => false,
    			'hierarchical' => true
    		)
    	);
    }

    Here is how I pull it into my custom archive template:

    //This is the slimmed down version
    <?php 
    
    	$args = array(
                'post_type' => 'our_properties',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                //'orderby' => 'post_date',
                //'order' => 'ASC',
                ); // END $args
            $my_query = null;
            $my_query = new WP_Query($args); ?>
    	<?php if ( $my_query->have_posts() ) : ?>
    
    			<div id="archive-container">
    				<!-- Start the Loop -->
    				<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    							<a href="<?php the_permalink(); ?>">View Full Details</a>
    						</div>
    					</div>
    
    				 <?php endwhile; ?>

    And I feel like it might help if I show you a sample link so here that is: http://hardisonbuilding.com/our-communities-new-home-builders-in-wilmington-north-carolina/

    Now if you go to that page and click on “Tarin Woods” for example, the next page you get to lists all the posts from all communities instead of just the posts associated with Tarin Woods.

    What am I doing wrong or how can I fix?

Viewing 6 replies - 1 through 6 (of 6 total)
  • First of all you want to make refer to the template hierarchy, and make sure your file naming conventions are accurate. For example your term template should be

    taxonomy-properties_community-tarin_woods.php

    Then I think you’ll want to modify your loop to:

    <?php
    // Modify Loop
    global $query_string;
    query_posts( $query_string . '&posts_per_page=-1');
    
    // Default Loop with Default Parameters
    if (have_posts()) :
        while (have_posts()) :
            the_post(); ?>
            <!-- Do Stuff -->
    <?php
        endwhile;
    else :
        echo '<p>'. _e('No Properties Found'). '</p>';
    endif;?>

    Thread Starter DesignLoud

    (@designloud)

    Thanks Christian, I knew I could create a template for each term but that would be difficult because what happens when client adds a new term and then there is no template for it. I guess what would be ideal was to query the database to see what term is being asked for and then display posts associated with that term. This way it would be more “dynamic” and no need to have a template that matches each term?

    Does that make sense or am I correct in thinking on that approach? Not sure how to implement it though, seems there should be an easier way to do this other than term-templates

    Thread Starter DesignLoud

    (@designloud)

    OR could I just create a taxonomy-properties_community.php template file and include a loop like you suggest and it will work the magic automatically by queuing the posts from whatever term is being queried?

    Ahh, I understand a little better now. Any TERM within properties_community taxonomy will use taxonomy-properties_community.php.

    What you are looking for is how to code BOTH an archive page for the properties_community taxonomy and an archive pages to handle each of the terms. To do this, you need a template for the post-type archive, and one to handle the terms.

    First, the archive where you display all the different communities. That should have been archive-our_properties.php. However, it looks like you’ve already modified a loop to display the same thing on this page.

    So all you really need to do is create,

    taxonomy-properties_community.php and use what I posted above. This will be the template used for all terms, ie, ‘communities’. If a new community (term) is added and attached to a post with posttype our_communities, it will use this template.

    The reason yours didn’t work is because you created a new WP_Query, that was looking for all the posts within the CPT our_properties. You don’t need a new Query on taxonomy archives, just use the query built by have_posts(). That’s the magic of WordPress.

    As long as you name your template correctly, just the default loop will get you 99% of the way there.

    Thread Starter DesignLoud

    (@designloud)

    Ahh, I got it now.. You rock dude, go have a drink on me 😉

    Thanks for your help

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Query posts from taxonomy’ is closed to new replies.