How can I get the ID of the current post/page/category in a way that matches the ID I get from menus using get_post_meta( $item->ID )['_menu_item_object_id'][0]? I intend to use it in a JavaScript function.

  • For pages, including the Posts Page, it is the page ID
  • For categories, it is the category ID

I have tried using global $wp_query; echo $wp_query->post->ID;, but it gives the ID of a post for category pages and the Posts Page. (first or last displayed post, depending on whether it is before or after the loop)

Read more here: Get page ID outside the loop, matching menu ID


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: