Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created January 7, 2015 16:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/6e275c7625be44af48aa to your computer and use it in GitHub Desktop.
Save tommcfarlin/6e275c7625be44af48aa to your computer and use it in GitHub Desktop.
[WordPress] Determine not only if a WordPress post has a term applied to it, but determine if the term is actively applied to it.
<?php
function acme_stocked_artifacts( $post_id ) {
// More to come...
}
<?php
function acme_stocked_artifacts( $post_id ) {
$stocked_artifacts = array(
'applied' => array(),
'not_applied' => array()
);
// More to come...
}
<?php
function acme_stocked_artifacts( $post_id ) {
$stocked_artifacts = array(
'applied' => array(),
'not_applied' => array()
);
// Get all of the artifacts for the post
$artifacts = wp_get_post_terms( $post_id, 'acme_artifacts' );
// More to come...
}
<?php
function acme_stocked_artifacts( $post_id ) {
$stocked_artifacts = array(
'applied' => array(),
'not_applied' => array()
);
// Get all of the artifacts for the post
$artifacts = wp_get_post_terms( $post_id, 'acme_artifacts' );
/* For each of the materials, determine which are applied and which are not
* and put them in the appropriate index of the array
*/
foreach ( $artifacts as $artifact ) {
// Check to see if the current term has children
$children = get_term_children( $artifact->term_id, 'acme_artifacts' );
if ( 0 < count( $children ) ) {
// If the first index of the children array isn't set, then we don't have any that are applied
if ( has_term( $children[0], 'acme_artifacts', $post_id ) ) {
array_push( $stocked_artifacts['not_applied'], $artifact );
} else {
array_push( $stocked_artifacts['applied'], $artifact );
}
// If there aren't any children, then we know it's applied.
} else {
if ( 'name of child' !== strtolower( $artifact->name ) ) {
array_push( $stocked_artifacts['applied'], $artifact );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment