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 this query to get posts and show like this:

Custom taxonomy 1
<div ID1>
 |-- post 1
 |-- post 2
</div>   
Custom taxonomy 2
<div ID2>
 |-- post 3
 |-- post 4
</div>

Now I need to get the current taxonomy ID for each section and insert to < div ID>. How to request & echo the ID in this code?

<?php

  $taxid = get_the_terms( $post->ID , get_cat_slug() );
    if($taxid) {
        foreach( $taxid as $xid ) {
            echo $xid->term_id;
        }
    }
  // for a given post type, return all
    $post_type = get_post_type();
    $tax = get_cat_slug();

    $tax_terms = get_terms(
        $tax, array(
            'hide_empty' => 1,
            'orderby' => 'name',
            'order' => 'ASC',
        ));

    if ($tax_terms) {
        foreach ($tax_terms as $tax_term) {
            $args = array(
                'post_type' => $post_type,
                "$tax" => $tax_term->slug,
                'post_status' => 'publish',
                'caller_get_posts' => 1,
                ); // END $args

            $my_query = null;
            $my_query = new WP_Query($args);

            if ($my_query->have_posts()) { ?>

                <div id="ID"> <!-- I need ID insert here -->

                <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
                  <!-- I need ID insert in HTML here -->
                <?php endwhile; ?>
                </div>

            <?php } // END if have_posts loop
            wp_reset_query();
        } // END foreach $tax_terms
    } // END if $tax_terms

    ?>

Thank you very much

share|improve this question

2 Answers 2

up vote 1 down vote accepted

In your foreach loop, the current term you are iterating is $tax_term:

foreach ($tax_terms as $tax_term) {

so within that block, the term's ID is $tax_term->term_id:

<div id="<?php echo $tax_term->term_id; ?>">
share|improve this answer
    
it works! Thank you :) –  Trung Kien Jul 13 '13 at 1:59
    
How do I get taxonomy link? <?php echo get_category_link( $tax_term->term_id ) ?> return nothing –  Trung Kien Jul 13 '13 at 3:41
    
echo get_term_link( $tax_term ) should work. –  Milo Jul 13 '13 at 3:57
$args = array('post_type'   => $post_type,
                'numberposts'   => $numberOfPost,
                'order'         => 'DESC'
            );
$tax_query = array(array('taxonomy' => $taxonomy,
                            'field'     => 'slug',
                            'terms'     =>$terms
$args['tax_query'] = $tax_query;

Here's a sample meta query. Use this incase you need meta query as well.

$meta_query = array('relation'      => 'AND',
                    array(  'key'       => $key_here,
                            'value'     => $value
                    ),
                    array(  'key'       => $other_key,
                            'value'     => $vaue,
                            'compare'   => 'BETWEEN',
                            'type'      => 'NUMERIC'
                    )
                );
$args['meta_query'] = $meta_query;

And Now pass $args in WP_Query

$posts = new WP_Query($args);

Let me know incase it doesn't help.

share|improve this answer

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.