-
-
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.
This file contains 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
<?php | |
function acme_stocked_artifacts( $post_id ) { | |
// More to come... | |
} |
This file contains 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
<?php | |
function acme_stocked_artifacts( $post_id ) { | |
$stocked_artifacts = array( | |
'applied' => array(), | |
'not_applied' => array() | |
); | |
// More to come... | |
} |
This file contains 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
<?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... | |
} |
This file contains 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
<?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