wordpress : category id and post id not getting wh

2019-08-27 04:53发布

问题:

How can I get both post id and category id in functions.php file ?

  function myFilter($query) 
    {

        if ($query->is_category)
        {
           //how to I get current caegories 
           //tried $currently_listing_categories = $query->query_vars['category__in'];  
but its not working when I turn on SEo friendly url
        }

        if ($query->is_single)
        {
           //how do I get current  id of the post
          //tried $thePostID = $query->query_vars['p'];
 but not working when I turn on SEO friendly url
        }
}

add_filter('pre_get_posts','myFilter');

I have to get currently_listing_categories and thePostID even after I turn on SEO friendly urls (Post name as permanent link)

Than you very much