1

I have a taxonomy called schedule_day_taxonomy

add_action( 'init', 'schedule_day_tax_func' );
function schedule_day_tax_func() {
    register_taxonomy(
        'schedule_day_taxonomy',
        'schedule',
        array(
            'hierarchical' => true,
            'label' => 'Day',
            'query_var' => true,
            'orderby'  => 'ID', /*
        )
    );
}

and this taxonomy is assigned to a certain custom post type only.. called schedule

add_action( 'init', 'schedule_func' );

function schedule_func(){
    register_post_type('schedule', array(
        'label' => 'TV schedule',
        'show_ui' => true,
        'supports' => array('title', 'thumbnail'),
        'public' => true,
        'publicly_queryable' => true, 
        'rewrite' => array( 'slug' => 'show-schedule' ),
        'labels' => array (
            'name' => 'TV schedule',
            'singular_name' => 'TV schedule',
            'menu_name' => 'TV Schedules'
        ),
    ) );
}

Now I want to get post by terms created under the taxonomy schedule_day_taxonomy which is assigned only for the custom post type schedule

I've created two terms monday and tuesday

I am trying to get all the posts if it is under the term tuesday. So I've tried following code:

<ul id="home-sche-list">
    <?php
        $args = array( 'post_type' => 'schedule', 'posts_per_page' => 15, 'tax_query' => array(
                array(
                    'taxonomy' => 'schedule_day_taxonomy',
                    'field' => 'tuesday',
                    'terms' => 'tuesday'
                )
            )                    
                                                );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();                           
        $image_id = get_post_thumbnail_id();
        $image_url = wp_get_attachment_image_src($image_id,'schedule_home_page_thumb', true);   
        $terms = get_the_terms( $post->ID, 'schedule_taxonomy' ); 
    ?>
    <li>
        <a href="<?php the_permalink(); ?>" target="_blank" class="wrapper-container">
            <div class="sche-thumb-con">
                <span>
                <img src="<?php echo $image_url[0]; ?>" class="video-thumb hoverZoomLink" alt=""/>                                      
                    <div class="new-cat">
                    <?php 
                        foreach($terms as $term) {
                          echo $term->name;
                        } ?>
                    </div>                                              
                </span>
            </div>
            <div class="sche-txt-wrapper">
                <div class="schedule-prog"><?php the_title(); ?></div>                              
                <div class="schedule-time evening">
                <?php 
                    $terms = get_the_terms( $post->ID, 'schedule_time_taxonomy' ); 
                    foreach($terms as $term) {
                          echo $term->name;
                        }                                           
                ?>
                </div>
            </div>
        </a>
    </li>
    <?php endwhile; ?>                              
</ul>

But I am getting no posts displayed. I have a post which is assigned to the term tuesday.

DAY

Cat

Admin address bar

address

What I am doing wrong? Will it affect if I assign this post to two more taxonomies (custom)? I have two more taxonomies and those are assigned to this post.

  • Anyone has any idea that why I got downvote for this question? – Foolish Coder Sep 10 '15 at 9:34
  • Upvoted you :-) – Pieter Goosen Sep 15 '15 at 7:21
1

The field parameter in the tax_query should be set to slug as it seems that you are passing the term slug. Any value passed to field except slug or name will cause the field parameter to default to term_id.

Always remember, the value passed to field should match the what is been passed to terms, so, if terms is term slugs, field should be slug

EDIT

Final solution from OP taken from comments

$args = array( 
    'post_type' => 'schedule', 
    'posts_per_page' => 15, 
    'tax_query' => array( 
        array( 
            'taxonomy' => 'schedule_day_taxonomy', 
            'field' => 'term_id', 
            'terms' => 1278 
        ) 
    )
);
  • This works for me.. $args = array( 'post_type' => 'schedule', 'posts_per_page' => 15, 'tax_query' => array( array( 'taxonomy' => 'schedule_day_taxonomy', 'field' => 'id', 'terms' => 1278 ) )); – Foolish Coder Sep 15 '15 at 6:52
  • My pleasure, glad it worked. Enjoy :-) – Pieter Goosen Sep 15 '15 at 6:59

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.