To get the current page ID outside of The Loop, you need to use get_queried_object_id();
($wp_query->
is unnecessary).
However, this will only work when you are viewing a single Page (so anything under Pages > All Pages in the back-end).
If you are viewing the blog, an archive, or a category it will not be a valid page ID. On categories it will return the category ID, but on archives or the blog it will not have a value because those things do not have an ID.
If your theme or another plugin uses query_posts()
, this could interfere with this function. Using wp_reset_query()
will fix this, but whichever theme or plugin is running query_posts()
should be doing this.
If you are using query_posts()
, don’t. Use a custom query with new WP_Query()
or get_posts()
.
Related Posts:
- How to: inspect global variables in WordPress
- get_post() vs global $post or $GLOBAL[‘post’]
- How to deal with global information without creating a Singleton class
- Using global $post v/s $GLOBALS[‘post’]
- Accessing $post global on a custom post type archive page
- get_query_var vs global query variables?
- What is the global $wp object used for?
- Change global values from templates
- Will $current_user no longer be global?
- How to avoid repeatedly use the new statement to instantiate a class? [closed]
- Is $page a global variable in wordpress?
- How can i use a global variable in a .css file generated with php?
- Echo get_the_category() outside of loop (global?)
- How does WordPress make its functions globally available?
- Recommendations on accessing current user data in WordPress
- WP user agent returns random variables
- How to get particular data from wp_list_comments outside the loop?
- How to save a viewer specific option
- What is the correct way to obtain access to the WP_oEmbed object?
- $GLOBALS[‘value1’] is not working
- Creating a (global)-mapping
- wp localize script makes variable global, how to solve that?
- using globals from wp_link_pages function
- Is there a way to natively return the full filesystem path when using $wp_styles?
- How to set and use global variables? Or why not to use them at all
- Should we trust the post globals?
- WordPress Multisite – global categories
- How to pass arguments from add_settings_field() to the callback function?
- What is a good alternative to using $content_width for image optimization?
- Passing variables from header.php to template and vice verca
- When to use global $post and other global variables?
- What is $post_id? is it a global variable in WordPress?
- WordPress Theme variables scope
- Any advantage of using wp_scripts and is_IE when enqueuing scripts
- Reducing the use of global $post
- Does an activated plugin automatically mean its methods are available to other WP functions?
- Where to get information about array fields in $_REQUEST?
- Global Objects and Public Methods
- Custom plugin: Trying to show saved data on frontend
- How to call a function only once (global variable scope)
- What is the difference between using global $current_screen and get_current_screen()?
- Why declare $post globally?
- Create a global variable for use in all templates
- When and Where is `global $post` Set and Available?
- Registering AJAX callback function that is part of a class without instantiating the class in function.php
- Filtered query_vars becomes global. Why does this work?
- query_posts() in function makes global $wp_query out of sync?
- How to set global variable in functions.php
- Passing variables to templates (alternatives to globalizing variables)
- Retrieve the template directory URI via global or get_template_directory_uri() every time?
- Detecting Embed URLs Within post_content
- using 1 form shortcode (si or cf7) for all multisite sites [closed]
- Filter the blog title displayed in the header
- Filter the blog’s title without using global variables
- How to access global $wp_meta_boxes variable on front-end?
- Multisite Installation: how do I setup global search?
- global $wp_meta_boxes returns NULL
- Save last login date in global before change it?
- How to check if this is the nth instance of a given shortcode in a post
- Passing variables to template parts
- Should I global $wpdb outside of any of my plugin’s functions scope?
- How should I store global information such as a phone number so that it is editable through the CMS?
- JavaScript, best way to use data from the loop
- store custom WP table names in a global variable
- WP 4.4.1 allow empty comments via add_action ‘pre_comment_on_post’
- Multisite Global Custom Posts
- Pass variables from one widget to another widget
- Adding a section to admin menu for global settings
- WordPress Multisite – global categories
- Using query_posts inside single.php loop
- How does the $post_type global variable work?
- How to fix a “globals” issue to avoid a rejected theme?
- after_setup_theme, Global Variable and Theme Customizer
- Global functions on WPMU
- How/where is the global variable $wp_registered_widgets filled?
- Add classname comment template from functions.php
- Cannot update custom database table row
- What are the disadvantages of using global variable?
- Defining a global array in functions.php?
- Global $post not working in OOP function WordPress
- Using global $post; to get featured image for custom post via WP_Query
- Call External Object in Class Function During Callback
- Ajax Pagination on Ajax filter
- Call global variable array() in woocommerce child/template
- Override global query results without hooks
- How to make global post work with custom post type?
- wp_get_current_user always returns 0 continued
- Getting $curauth->ID to work inside a shortcode
- WordPress Loop and $post
- Result of API Request to global variable?
- How to prevent XSS alter custom global javascript object & methods in WordPress
- Getting List of all registered Dashboard Widgets
- Create a new query in function.php to filter blog posts
- What is the proper method of using global $post?
- Relative redirects for posts, categories, products and product categories
- global variables in templates
- How to access plugin variables from theme templates without using globals?
- How to modify a global variable in a function and use it on another function?
- Assign External Database Queries to Global Variables and Make Them Accessible
- Global $post shows null in some of my custom post types archive pages