Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using custom taxonomies.

How to check if current page has tag_ID = "XXX" ?

If that is any help the URL form WordPress admin looks like this:

http://localhost/website/wp-admin/edit-tags.php?action=edit&taxonomy=company&ta‌g_ID=13&post_type=news 


Update. Here is code which I'm currently using. Does not work.

<?php
function insert_news($company = 0)
{
    global $post;

    $count       = 0;
    $news_query  = new WP_Query(array(
        'post_type' => 'news',
        'posts_per_page' => -1,
        'orderby' => 'date',
        'order' => 'ASC',
        'company' => $company
    ));
    $total_count = $news_query->post_count;
    while ($news_query->have_posts()):
        $news_query->the_post();

        $count++;

        $tag_id = get_query_var('tag_id');

        if ($tag_id && $tag_id == '14') {
            // ID 14

        } else if ($tag_id && $tag_id == '13') {
            // ID 13


        } else if ($tag_id && $tag_id == '12') {
            // ID 12


        }
    endwhile;
}
?>
share|improve this question

6 Answers 6

up vote 3 down vote accepted
+100

All the tags is inside the query, it is possible to check for this via get_query_var(); all objects inside the query. For the tag ID ask with this code: get_query_var( 'tag_ID' ); Also it is possible to check for tags, like get_query_var( 'term' ) and the taxonomy get_query_var( 'taxonomy' )

So you can create an Loop with your tag data form your taxonomy; like

$wpq = array ( 
    'post_type' => 'post',
    'post_status' => 'published',
    'taxonomy'=> get_query_var( 'taxonomy' ),
    'term'=> get_query_var( 'term' )
);
$query = new WP_Query( $wpq );
while ( $query->have_posts() ) : $query->the_post();

So you can check: if ( 'your_tad_id=== get_query_var( 'tag_ID' ) ) `

  • An hint; if you will use the check via $_REQUEST-var; please filter this data or use only for if statements.
share|improve this answer
if ( is_object_in_term( $post->ID, 'post_tag', 'tag_slug_or_name_or_id_or_array' ) ) 
   do_my_funky_stuff();

Update: Regarding the following comment:

How to check if current page has tag_ID = "XXX" ?

If that is any help the URL form WordPress admin looks like this:

...edit-tags.php?action=edit&taxonomy=company&ta‌g_ID=13&post_type=news

So I'm guessing you mean "check if editing tag with ID X"?

if ( isset( $_REQUEST['tag_ID'] ) && $_REQUEST['tag_ID'] == X )
    do_my_funky_stuff();

You really must be a little clearer with your teminology ;)

share|improve this answer
    
Hmm.. it does not work. If that is any help the URL looks like this http://localhost/website/wp-admin/edit-tags.php?action=edit&taxonomy=company&ta‌​g_ID=13&post_type=news –  User789 Jul 5 '12 at 19:46
    
@NewUser can you show the exact code that you used? –  Pekka 웃 Jul 6 '12 at 7:37
    
@Pekka - please see the update. Have not got a clue why this does not work. –  User789 Jul 11 '12 at 22:03
    
Sorry, for the edit. I rolled it back - my mistake, with string/int on tag ID. –  kaiser Jul 18 '12 at 13:27
    
ten thousand thanks for this help! –  whiteletters and blankspaces Jun 27 '13 at 12:10

If you're only interested in 3 subsequent tag_id's you could start with narrowing down your query, using $_POST['tag_id'] and 'tag__in':

$query_tag_id = $_POST['tag_id'];

$news_query  = new WP_Query(array(
    'post_type' => 'news',
    'posts_per_page' => -1,
    'orderby' => 'date',
    'order' => 'ASC',
    'company' => $company,
    'tag__in' => array( $query_tag_id-1, $query_tag_id, $query_tag_id+1 )
));

And you can refer back to the $query_tag_id variable in the if conditional tests, like:

if( $tag_id == ( $query_tag_id - 1 ) ) {
    // do stuff
}
if( $tag_id == $query_tag_id ) {
    // do stuff
}
if( $tag_id == ( $query_tag_id + 1 ) ) {
    // do stuff
}

I'd expect that the suggestion made by TheDeadMedic would do the trick when you combine it with the above. Like this for the middle if test:

if ( is_object_in_term( $post->ID, 'post_tag', $query_tag_id ) ) 
   do_my_funky_stuff();

Failing that you could always get all the tag ids for the active post in the while loop and use those in the if tests. So you'd have to replace

$tag_id = get_query_var('tag_id');

with:

$tags = get_the_tags();
$tag_ids = array();
if( $tags ) {
    foreach ( $tags as $tag ) {
        $tags_id[] = $tag->term_id;
    }
}    

if ( in_array( ( $query_tag_id - 1 ) , $tag_ids ) ) {
    // do stuff
}
etc.

If you're certain your posts are only going to have one tag, you could simplify things by using:

$tags = get_the_tags();
$tag_id = ( $tags ) ? $tags[0]->term_id : 0;

if( $tag_id == ( $query_tag_id - 1 ) ) {
    // do stuff
}
etc.

Update: On a side note, get_query_var() does not work because it looks for the parameters you set in the query. You didn't set any tag_id in the query, so it does not return anything. So changing the query as I suggested at the start of my answer would get you a return value, but not one you could use.

share|improve this answer

Haven't tested this, but it should work.

<?php
// Gets the current Post ID
function get_postID(){
    global $wp_query;
    $thePostID = $wp_query->post->ID;
    return $thePostID;
}

// Check for specified Term ID
function check_term_id($id){
    $postID = get_postID();
    $post = get_post($postID);
    $taxonomies = get_object_taxonomies($post->post_type);
    foreach($taxonomies as $taxonomy){
        $terms = get_the_terms($post->ID, $taxonomy);
        if(!empty($terms)){
            for($i = 0; $i < count($terms); $i++){
                if($terms[$i]->term_id == $id){
                    $return = $terms[$i];
                } else{
                    $return = 'error';  
                }
            }
        }
    }
    return $return;
}

$checkTerm = check_term_id('xxx');
if($checkTerm != 'error'){
    //Do something with the term data.
}
?>
share|improve this answer

Try globalizing $wp_query since your doing this within a function.

share|improve this answer
$url = "http://localhost/website/wp-admin/edit-tags.php?action=edit&taxonomy=company&ta‌g_ID=13&post_type=news";
if (strpos($url, "tag_ID=13")!==false){
     //Do stuff here if it is there
}
else {
     //Do Other Stuff here if it is not there
}

For WordPress' sake, throw that into a function in the functions.php and add a hook, like init (Bare in mind this would be specific just to that tag) You could make it conditional by setting a couple variables and testing to see if it equals a particular value.

share|improve this answer
    
sorry. Small misunderstanding. The URL is coming from wordpress admin site. Not the site front-end. Thanks for your answer both using FIXED URL as variable is too "manual". –  User789 Jul 6 '12 at 7:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.