0

I am using the Custom Post Type feature in Wordpress 3 to create an Events section for my website. I am able to get this to display by using the relevante template files, but I also want the event listings visible on other pages.

I've done this with the normal post types but these are based on category IDs which Custom Post Types don't have.

Each event has a taxonomy for country and I want to be able to loop through the events and display only events for specific countries so I want to be able to filter that.

I've had a look on the Codex and came up with the following but it doesn't display anything:

    <?php $args = array(
                'post_type'=> 'events',
                'country'    => 'england'
          );
            $the_query = new WP_Query( $args );           
            if ( $the_query->have_posts() ) :

            // The Loop
            while ( $the_query->have_posts() ) : $the_query->the_post();
                echo '<li>';
                the_title();
                echo '</li>';
            endwhile;

            endif;

            // Reset Post Data
            wp_reset_postdata();
    ?>
2

I've figured out the answer using the Wordpress forum. Solution below:

        <?php query_posts( array( 'country' => 'event-england' ) ); ?>
        <?php if( is_tax() ) {
            global $wp_query;
            $term = $wp_query->get_queried_object();
            $title = $term->name;
        }  ?>

        <ul>
        <span class="tax-title"><?php echo($title); ?></span>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><?php the_excerpt() ?></li>

        <?php endwhile; else: ?>
        <?php endif; ?>
        </ul>
| improve this answer | |
1

Every taxonomy (built in like 'tags', 'post-formats', 'category', as well as custom) have an ID, so you can get this one as well.

As far as i can see your query should work if you just add the usual if ( $the_query->have_posts() ) : before your loop and endif; after it.

| improve this answer | |
  • I have followed your suggestion but still nothing showing? Please see updated code in my original post. – thisisready May 24 '11 at 10:20
0

This Worked For Me

I hope it helps you or anyone else coming across this "issue"...

So, assuming you registered your Custom Post Type as well as your Taxonomy(I registered mine in functions.php) --> How to register your CPT and Taxonomy

On the page I wanted to display the posts from that taxonomy I used this code...

<div class="the-div-you-want-to-loop-in">

<?php $query = new WP_Query(array(
        'post_type' => 'your-post-type', // Your Post Type
        'posts_per_page' => -1, // To Display All Posts
        'tax_query' => array(
          array(
            'taxonomy' => 'your-taxonomy',
            'field'    => 'slug',
            'terms' => 'your-filter', // (the name of what you want to filter by (latest or whatever))
          )
      ))); ?>

      <?php if($query->have_posts()): while($query->have_posts()): $query->the_post(); ?>

        <div class="my-post">
            <?php responsive_img(get_post_thumbnail_id(), 'large'); ?>
            <h2><?php the_title(); ?></h2>
        </div>

      <?php wp_reset_postdata(); ?>
      <?php endwhile; endif; ?>

</div>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.