6

I have a custom post type, called articles. I've also created a taxonomy called subject. Each article has a subject, just like posts have categories.

Now to the problem. When I'm on the page-articles.php (page-slug.php), it has to check which subject the article has. How can I do that?! I've been surfin' the web for days without finding any solution.

What I'm thinking is that in some way I have to check the taxonomy subject if it matches a criteria. Like:

if (is_term('news', 'subject'){
// do this
}

But I really can't figure out how it works! The is_term doesn't do that, and I can't find any other function either.

  • 2
    Use has_term ? – t31os Mar 30 '11 at 10:08
  • Just to clarify, are you asking how to check to see if a specific subject is associated with a given article, or a means by which to list the subject(s) for an article? I've seen answers for both listed in the responses, and it's unclear as to which ones address your situation best without knowing. – Justin Bell Sep 29 '14 at 22:37
10
if( has_term( 'jazz', 'genre' ) ) {
    // do something
}

http://codex.wordpress.org/Function_Reference/has_term

  • 6
    @user4310, The first parameter is the term you are looking for and the second parameter is the taxonomy that term is in. – s_ha_dum Dec 13 '12 at 0:25
3

I tried the code below and it works -- is this what you're looking for?

<?php
    // grab your custom post type and output them all
    query_posts('post_type=articles&posts_per_page=-1');

    if ( have_posts() ) : while ( have_posts() ) : the_post();

        // you can output your title, permalink, etc. anywhere within the loop

        // get all items in your custom taxonomy
        $terms = get_the_terms($post->ID, 'Subject');

        // loop through each term and perform your check
        foreach ( $terms as $term ) {
            if($term->name == 'Term you are checking for') {
                // do stuff here
            }
        }
    endwhile; endif;
?>

Replace 'Term you are checking for' above with the actual name of the subject you are checking against, not the slug.

Edit: I'm no expert on database performance, so not sure if this is the optimal way to retrieve the information you're after -- I'm open to others' input.

1

I've had an interesting struggle with this myself.

I'm using http://codex.wordpress.org/Function_Reference/get_the_terms http://codex.wordpress.org/Function_Reference/is_object_in_term

I had to display category headings and other meta data depending on the presence of taxonomy tags.

I got unpredictable results with is_object_in_term checking for an ARRAY of specific tags, but maybe what I did is of use to someone else. I ended up getting it working by using NULL, but I just needed to check if a taxonomy existed and ANY terms under it applied to the post being viewed so I could echo out certain display elements.

I'm assuming if you're just hunting for ONE term the stock examples on the codex page should help out. Something like...

if ( is_object_in_term( $post->ID, 'news', 'subject' ) ) :
echo 'YES';
else :
echo 'NO';
endif;

My relevant code below:

 $heading_nicename = array('Category','Neighborhoods','Zip Codes','Member Agencies','Nearest Public Transportation');
 $tax_slug = array('category','neighborhoods','zip_codes','member_agencies','nearest_public_transportation');
 $heading_key = 0;
 $termindex = 0;`

  foreach ( $tax_slug as $taxonomy ) {
  //used to spit out terms as links
  $object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));

  //used in a check to see if tax terms even apply to post
  $o_terms = get_the_terms( $post->ID, $taxonomy );

  if ( $o_terms && ! is_wp_error( $o_terms ) ) {
    $check_these_terms = array();
  }

  foreach ( $o_terms as $term ) {
    $check_these_terms[] = $term->slug;
    //echo $term->slug.'<br/>';
    }

    $o_list = join( ", ", $check_these_terms );
    //echo 'taxonomy: '.$taxonomy.'<br/>';
    //echo 'terms: '.$o_list.'<br/>';

 //unpredictable results
 //if ( is_object_in_term( $post->ID, $taxonomy, array($o_list ) ){
    //echo '<h4>'.$heading_nicename[$heading_key].'</h4>';
    //}

 //Ends up being a check for ANY term under a taxonomy
    if ( is_object_in_term( $post->ID, $taxonomy, null ) ){
    echo '<h4>'.$heading_nicename[$heading_key].'</h4>';
    }

    $heading_key++;
    echo '<p>';

$endindex = count($object_terms);
$termloop = 0;
foreach ($object_terms as $term) {

  echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a>';
  //clean up commas
  if ($termloop !== ($endindex - 1)){
    echo ', ';
    $termloop++;
  }
  $termindex++;
}
    echo '</p>';

  }`
  • the first portion of code worked for my case. – breezy Apr 19 '17 at 20:26
0

Have you tried using wp_get_object_terms?

Something along the lines of

$article_subjects = wp_get_object_terms($post->ID, 'subject');
0

if each article has a subject, you don't need to check whether it has a subject, right? Then you can use something like this:

<?php echo get_the_term_list( $post->ID, 'subject', __('Subject: ', 'theme-name'), '  ', '' ); ?>

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.