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.
<?php
/*
Plugin Name: Members
*/

   function myFilter2($query) 
    {

        if ($query->is_category)
        {
            $currently_listing_categories = $query->query_vars['category__in'];             
            print_r($currently_listing_categories);
        }

    }

    add_filter('pre_get_posts','myFilter2');
?>

This plugin display the category ids when the url is not SEO friendly

http://domain.com/wplab/wpla4/?cat=4

. but when I turn on SEO

http://domain.com/wplab/wpla4/category/members/

the array is empty

how can I get the category id with SEO friendly urls

share|improve this question

2 Answers 2

Use this function to get current cateogry in wp :

function getCurrentCatID(){

 global $wp_query;
 if(is_category() || is_single()){
  $cat_ID = get_query_var('cat');
 }
 return $cat_ID;

}

echo getCurrentCatID();

Just found for you, try this,

if(isset($wp_query->get_queried_object()->cat_ID))
{
    $cur_catId = $wp_query->get_queried_object()->cat_ID;
}
if(issset($wp_query->get_queried_object()->ID))
{
    $cur_postId = $wp_query->get_queried_object()->ID;
}
share|improve this answer
function myFilter2($query) 
    {

        if ($query->is_category())
        {
            $currently_listing_categories = $query->get('cat');             
            print_r($currently_listing_categories);
        }

    }

    add_filter('pre_get_posts','myFilter2');

you should use

1, $query->is_category() instead of accessing the is_category variable directly.

2, $query->get() to get query value.

Just one tip, you can always use the code below to output the contents.

echo '<pre>';
print_r($query);
exit;
share|improve this answer

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.