Created
June 5, 2013 13:19
-
-
Save steweir/5713795 to your computer and use it in GitHub Desktop.
Get posts with custom taxonomy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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 pa_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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (pa_in_taxonomy('genres', 'western')) { | |
| echo "Western films are great"; | |
| } else { | |
| // do something else | |
| } | |
| // Note I’m using the tax/term slug, but the is_object_in_term function in use will accept term id, slug, or array of either, so you could do: | |
| if (pa_in_taxonomy('genres', array('western', 'drama', 'comedy'))) {} | |
| // or | |
| if (pa_in_taxonomy('genres', array(1, 2, 3)) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment