WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Nov 14 2011

in_taxonomy(): Check to Determine Whether a Post Is in a Specific Custom/Builtin Taxonomy

I found this great code snippet by Alex Leonard.

[php]
<?php

/**
* Conditional function to check if post belongs to term in a custom taxonomy.
*
* @param tax string taxonomy to which the term belons
* @param term int|string|array attributes of shortcode
* @param _post int post id to be checked
* @return BOOL True if term is matched, false otherwise
*/
function wps_in_taxonomy($tax, $term, $_post = NULL) {

// if neither tax nor term are specified, return false
if ( !$tax || !$term ) { return FALSE; }

// if post parameter is given, get it, otherwise use $GLOBALS to get post
if ( $_post ) {
$_post = get_post( $_post );
} else {
$_post =& $GLOBALS['post'];
}

// if no post return false
if ( !$_post ) { return FALSE; }

// check whether post matches term belongin to tax
$return = is_object_in_term( $_post->ID, $tax, $term );

// if error returned, then return false
if ( is_wp_error( $return ) ) { return FALSE; }

return $return;
}
[/php]

Written by Travis Smith · Categorized: WordPress

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. Alex Leonard says

    November 15, 2011 at 1:25 am

    Hey Travis,

    Great to see you found this little function useful!

    Thanks for the re-share 🙂

    Alex

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in