0

I have changed my set-up slightly to have 2 separate post types "Premium Listing" and "Free Listing" and a taxonomy for Listing_category.

Here is an example of my Loop The idea is to display Premium listings followed by Free listings on the archive page of my taxonomy.

This seems to work however when I add it to my taxonomy_listing_category.php I get results that are not specific to my taxonomy.

For instance http://www.domain.com/listing_category/carriers/ should show 1 Premium and then Free listings with taxonomy Carriers, however it shows all taxonomies.

The code is below

<?php   $args = array(
    'post_type' => 'premium_listing',
    'orderby' => 'rand',
    );
    $the_query = new WP_Query( $args );
?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) :     $the_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; else: ?>
<?php get_template_part( 'inc/posts/post-404' );?>
<?php endif; ?>

<?php rewind_posts(); ?>

<?php   $args = array(
    'post_type' => 'free_listings',
        'post_per_page' => 1,
    'orderby' => 'rand',
    );
    $the_query = new WP_Query( $args );?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) :     $the_query->the_post(); ?>
<?php the_title() ;?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>

My theme doesnt have an archive.php so I have used the index.php as the basis for creating taxonomy_listing_category.php

Any ideas Richard.

Richard

0

Did you post the correct name for the taxonomy template? I think it should be ' taxonomy-listing_category.php. See Custom_Taxonomies_display

Or are you including it some other way?

With new WP_Query you're not using the default query which has the query variables for the taxonomy and term. You can however add the taxonomy and it's term back to the queries to have it only return posts that have the term from the taxonomy assigned to it.

See the tax_query parameter

<?php
$current_term = get_query_var( 'term' );
$args = array(
    'post_type' => 'premium_listing',
    'orderby'   => 'rand',
    'tax_query' => array(
        array(
            'taxonomy' => 'listing_category',
            'field'    => 'slug',
            'terms'    => $current_term ,
        )
    )
);
$the_query = new WP_Query( $args );
?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) :     $the_query->the_post(); ?>
<?php the_title(); ?>
<?php
endwhile;
wp_reset_postdata();
else: ?>
<?php get_template_part( 'inc/posts/post-404' );?>
<?php endif; ?>

<?php   $args = array(
    'post_type'     => 'free_listings',
    'post_per_page' => 1,
    'orderby'       => 'rand',
    'tax_query'     => array(
        array(
            'taxonomy' => 'listing_category',
            'field'    => 'slug',
            'terms'    => $current_term ,
        )
    )
);
$the_query = new WP_Query( $args );?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) :     $the_query->the_post(); ?>
<?php the_title() ;?>
<?php
endwhile;
wp_reset_postdata();
else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
| improve this answer | |
  • Yes i was using taxonomy-listing_category.php, I spelt it wrong in this post. It seems to be working now using your code Thanks Richard – Richard Ascough Jun 30 '14 at 8:13

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.