test if something is a post or a taxonomy

I have a custom post stored inside a $post_or_term variable, and I’m trying to write an IF statement that tests whether or not the variable holds a post or a taxonomy.

I have tried using is_single() and is_singular() as the test, but neither of them resolve as “true”.

The code works fine if I strip away the IF statement, so that is definitely the cause of the problem… I just have no idea why.

The array below is the custom post stored inside my $post_or_term variable (taken from a var_dump($post_or_term) ):

array(24) { [“ID”]=> string(4) “1079” [“post_author”]=> string(1) “1” [“post_date”]=> string(19) “2018-05-14 10:41:21” [“post_date_gmt”]=> string(19) “2018-05-14 10:41:21” [“post_content”]=> string(0) “” [“post_title”]=> string(9) “Australia” [“post_excerpt”]=> string(0) “” [“post_status”]=> string(7) “publish” [“comment_status”]=> string(4) “open” [“ping_status”]=> string(4) “open” [“post_password”]=> string(0) “” [“post_name”]=> string(9) “australia” [“to_ping”]=> string(0) “” [“pinged”]=> string(0) “” [“post_modified”]=> string(19) “2018-05-15 08:50:39” [“post_modified_gmt”]=> string(19) “2018-05-15 08:50:39” [“post_content_filtered”]=> string(0) “” [“post_parent”]=> string(1) “0” [“guid”]=> string(47) “https://www.datafreedoms.com/country/australia/” [“menu_order”]=> string(1) “0” [“post_type”]=> string(7) “country” [“post_mime_type”]=> string(0) “” [“comment_count”]=> string(1) “0” [“pod_item_id”]=> string(4) “1079” }

And this is my IF statement…

function bg_get_points ($post_or_term) {
// Checks for a taxonomy
if (is_tax($post_or_term)) {
$id = $post_or_term[ ‘term_id’ ];
$term_points = get_term_meta( $id, ‘points’, true );
if ( ! empty($term_points) ) {
return $term_points;
}
// Checks for a post
} elseif (is_single($post_or_term)) {
$id = $post_or_term[ ‘ID’ ];
$post_points = get_post_meta( $id, ‘points’, true );
if ( ! empty($post_points) ) {
return $post_points;
}
}
}

Any ideas whats wrong?

Read more here:: test if something is a post or a taxonomy

Leave a Reply

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