0

I'm trying to test the term for a post within a wp-query and for-loop. Everything works great however testing the terms via get_the_terms($post->ID, 'category') returns an empty array.

$terms = get_the_terms($post->ID, 'category');  // $terms is []

I know at least one of my posts have a category called fact-sheet attached. What is get_the_terms missing?

Should I be adding taxonomy arguments to my WP_Query args in order to access them more easily?

$args = array(
    'posts_per_page'  => 10,
    'paged'           => $paged,
    'orderby'         => 'post_date',
    'order'           => 'DESC',
    'post_type'       => $_POST['post_type'],
    'post_status'     => 'publish',
    'suppress_filters' => true,
);  

Also, is there a very simple way to query a term name by ID? Perhaps with get_posts

UPDATE: Turns out using $post->ID like $terms = get_the_terms( $post->ID , 'category' ); breaks get_the_terms and a hardcoded integer as the post id works... What could be wrong with $post->ID ?

Please see the Switch / casestudy argument for examples.

Here is the full query function:

function ajax_paginate() {
check_ajax_referer( 'util-ajax-nonce', 'nonce');


// if query_vars exists use it! - only from a click on pagination links
// else, a standard query - page 1
if(isset($_POST['query_vars'])){
    $args               = json_decode( stripslashes( $_POST['query_vars'] ), true );
    $args['paged']      = intval( $_POST['page'] );
    $args['post_type']  = $_POST['post_type'];   
}
else
{
    // this will eventually be dynamic
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $args = array(
        'posts_per_page'  => 10,
        'paged'           => $paged,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_type'       => $_POST['post_type'],
        'post_status'     => 'publish',
        'suppress_filters' => true,
    );        
}

$data = new WP_Query( $args );
$array = array();
$i = 0;

foreach ($data->posts as $post) {
    $array[$i]['title'] = $post->post_title;
    $array[$i]['type']  = $post->post_type;
    $array[$i]['date']  = get_the_date('F j, Y', $post->ID);

    if($post->post_type == 'news'){
        $author = get_post_meta($post->ID, '_util_news_website', true) != '' ? get_post_meta($post->ID, '_util_news_website', true) : 'Util';
    }
    else if( $post->post_type == 'post' ){
        $author = get_post_meta($post->ID, '_util_blog_custom_author', true) != '' ? get_post_meta($post->ID, '_util_blog_custom_author', true) : 'Util';
    }

    switch ( $post->post_type ) {
        case 'news' :
            $array[$i]['preheading'] = 'News Item';
            $array[$i]['permalink']  = get_post_meta($post->ID, '_util_news_url', true) != '' ? get_post_meta($post->ID, '_util_news_url', true) : '#';
            $array[$i]['by']         = " by <span class='author' itemprop='author' itemscope itemptype='http://schema.org/Person'>" . $author . "</span>"; 
        break;
        case 'press' :
            $array[$i]['preheading'] = 'Press Release';            
            $array[$i]['permalink']  = get_post_meta($post->ID, '_util_press_pdf_url', true) != '' ? get_post_meta($post->ID, '_util_press_pdf_url', true) : '#';
            $array[$i]['excerpt']    = substr($post->post_content, 0, 455);
            $array[$i]['by']         = "";

        break;
        case 'post' :
            $array[$i]['preheading']    = 'Blog Article';
            $array[$i]['permalink']     = $post->post_name; 
            $array[$i]['featuredImage'] = get_the_post_thumbnail($post->ID, 'blog-thumb'); // gets any thumbnail - $attr is optional - $post->id, $size, $attr 
            $array[$i]['excerpt']       = get_excerpt_by_id($post->ID, 50, '', ''); // substr($post->post_content, 0, 455);
            $array[$i]['by']            = " by <span class='author' itemprop='author' itemscope itemptype='http://schema.org/Person'>" . $author . "</span>";
        break;
        case 'casestudy' :
            //$terms = get_the_terms( $post->ID, 'category' ); // does not work
            $terms = get_the_terms( 430, 'category' ); // works
            $array[$i]['preheading'] = $terms[0]->name;
            $array[$i]['permalink']  = get_post_meta($post->ID, '_util_casestudy_pdf_url', true) != '' ? get_post_meta($post->ID, '_util_casestudy_pdf_url', true) : '#'; 
            $array[$i]['by'] = '';
        break;

        // the else statement
        default:
            $array[$i]['permalink'] = $post->permalink;            
        break;
    }

    $i++;
}
  • Did you register a taxonomy or did you register the default categories onto your Custom Post Type? If you registered a custom taxonomy, replace category with your taxonomy slug. – Howdy_McGee Dec 16 '15 at 15:55
  • What is get_the_terms missing The correct taxonomy ;-) – Pieter Goosen Dec 16 '15 at 15:56
  • Are you sure that array is empty? Try this: foreach ( $my_tax as $tax ) { $field = $tax->name; } and print_r($field); Obviously replace $my_tax with your category variable. You can also replace name with ID if you want to output that. – N00b Dec 16 '15 at 16:07
  • 1
    @Howdy_McGee Where I registered my CTP is register_taxonomy_for_object_type( 'category', 'casestudy' ); @N00b trying to loop through seems to break the query... – Ben Racicot Dec 16 '15 at 16:29
  • Oops, try this: foreach ( $my_tax as $tax ) { $field = $tax->name; break; } There should be a break; in that foreach loop. – N00b Dec 16 '15 at 17:49

Your Answer

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

Browse other questions tagged or ask your own question.