Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a section on my main page that displays a new “Portfolio” post each time I write one no matter what category I assign them to. What I want to do is make the main page only display one specific category (events) from these Portfolio posts. I’m just wondering what I need to change in the below code to do that?

$args_for_latest = array(
    'numberposts'     => 5,
    'offset'          => 0,
    'orderby'         => 'port_date',
    'order'           => 'DESC',
    'post_type'       => 'port',
    'tax_query'       => array(  
                            'taxonomy' => 'portcat',  
                            'field' => 'slug'
                        ),
    'post_status'     => 'publish' 
); 


$temp_latest = $wp_query;
$wp_query_latest= null;
$wp_query_latest = new WP_Query();
$wp_query_latest->query($args_for_latest);
while ($wp_query_latest->have_posts()) : $wp_query_latest->the_post();
$featured_image_latest = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );

$terms = get_the_terms($post->ID, 'portcat');
$array_keys = array_keys($terms);

?>
    <li><center><?php if ($featured_image_latest[0]!=="") {
        echo "<img src='".TIMTHUMBURL."?w=120&h=120&src=".$featured_image_latest[0]."' alt='".get_the_title()."'>"; 
        } else { ?>
        <img src="<?php echo IMGURL; ?>/pic_article1.jpg" alt="" />
        <?php } ?>
        <span><?php  echo $terms[$array_keys[0]]->name;  ?></span>
        <h4><?php the_title(); ?></h4>
        <?php echo substr(get_the_excerpt(), 0, 70)."..."; ?></center>
        <a href="<?php echo get_permalink($post->ID); ?>" class="read">Read more</a>                            
    </li>
<?php endwhile; wp_reset_query(); ?>
share|improve this question
2  
Have you tried reading codex.wordpress.org/Class_Reference/WP_Query –  Wyck Apr 8 '13 at 3:03
    
Oh thank you, that was perfect! Similarly, I'd like to display all portfolio categories but EXCLUDE 'events' in another section. I've tried the below and a few variations but it didn't work? $args_for_latest = array( 'numberposts' => 5, 'offset' => 0, 'orderby' => 'port_date', 'order' => 'DESC', 'post_type' => 'port', 'post_status' => 'publish' 'tax_query' => array( 'taxonomy' => 'portcat', 'terms' => 'events' 'field' => 'slug', 'operator' => 'NOT IN' ) ) ); –  Sam Apr 9 '13 at 1:37
    
Ask a new question. Please don’t misuse the answer field for questions. Thanks. –  Tom J Nowell Feb 1 at 18:32
add comment

2 Answers

Wyck is correct. You can find the answer you seek in the wonderful Wordpress documentation.

But, 'cause why not, here's the answer for you:

You're very close, you just need to change your "tax_query" array to include the term you're requesting. Also, tax_query only accepts arguments in the form of arrays, so you end up with an array of arrays. Like this:

    'tax_query' => array(
                    array(
                      'taxonomy' => 'portcat',
                      'field' => 'slug',
                      'terms' => 'events'
                    )
                  )
share|improve this answer
add comment

Maybe this will help.

$args_for_latest = array(
    'numberposts'     => 5,
    'category_name'   => 'events', ///Use category slug,not name.
    'offset'          => 0,
    'orderby'         => 'port_date',
    'order'           => 'DESC',
    'post_type'       => 'port',
    'tax_query'       => array(  
                            'taxonomy' => 'portcat',  
                            'field' => 'slug'
                        ),
    'post_status'     => 'publish' 
);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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