The $post
global is set by $wp_query->the_post()
. As $post
is set by the main query and the fact that templates are set within the global variable scope, it is available throughout the template, so there is no need to explicitly calling the $post
global when you are within the specified template as the WP_Post
objects can be accessed any where. You can access the WP_Post
objects by just simply calling them directly, for example $post->ID
to retrieve the page ID on a page template
When you need to access post data outside of a template, for example, in a function or in a widget, you need to call the $post
global to make the post data available for use.
Related Posts:
- Ignoring initial articles (like ‘a’, ‘an’ or ‘the’) when sorting queries?
- Wp_redirect and sending variables
- How do I get posts that have a thumbnail in WP_Query?
- Multiple WP_Query loops with Pagination
- how does $wpdb differ to WP_Query?
- WP_Query – filter or directly?
- WP_Query with checkbox meta_query
- IP address character limit
- What is the most efficient way of querying posts based on visits and date for current day?
- How can I save an array from a random post sequence for later use?
- Does WordPress generate an automatic page for post formats?
- Why is WP-Query spelled like it is?
- How to add custom query filters in WP_User_Query
- How to order category.php loop by ‘meta_value’?
- Searching user meta using WP_User_Query
- Which custom query am I in and how can I access its properties & methods?
- Pagination not working Search posts
- Does using a custom query_var create a security hole?
- How can i make WP_Query return post image and categories?
- Filter WordPress posts by between parameter
- Get image of latest post from taxonomies/categories
- 2 loops on page – one with orderby rand second orderby date
- Undefined WP_Query::has_posts()?
- WP_Query returning no results
- Splice together 2 WP_Query objects
- pre_get_posts Remove tax_query Completely
- How to access a query twice in different template files?
- Counter problem, infinite loop when post_per_page equals X
- Custom query based on meta key – Reduce three states to two in results?
- How to create a WordPress Post Filter that users can use to filter posts by year?
- Exclude category for main query and custom
- Complex Category selection as per user input
- How to set multiple `orderby` in query argument?
- WP_Query posts_per_page Only Bringing Back one post
- How to get all unique categories for posts in loop?
- Query that loads a custom type AND posts whose ids are not inside those custom types custom fields
- Meta_query with multiple keys and multiple values
- WordPress WP_Query make mysql time out
- Get authors by term id or slug
- Get and Trim Full Post Content in WP Query
- Transient not behaving as expected
- how can I override The Events Calendar query modifications?
- New WP_Query calls Pre_get_posts filter twice
- Multisite wp_query & switch_to_blog issue
- the_post() within switch_to_blog() altering my excerpt
- No results found from a $wpdb->get_results() query when trying to join more than one meta key query
- WP_Query post_parent parameter always returns children of current page
- Query about wp_query
- wp_query sort with meta_value_num is not working
- empty WP_Query object on local install
- Changing the default wp_search_stopwords
- Query posts only without featured image
- How to use WP_Query() on single.php?
- Tax Query only returns for the first of several terms
- Can’t fetch Custom Post Type Data through Custom Query
- Loop increase in while loop not working
- Get All posts by Tag_Id not working, query seems to contain extra term_taxonomy_id
- Is this meta query problematic?
- Appending to existing WP_Query’s meta_query if exists
- Yoast primary category query modification
- How To Keep Search Title the same on paged Results
- Excluding posts with certain value of meta_key keeping posts without that meta_key
- Get next/prev image/attachment in time with date query
- Filter out a meta key in the Search results page with two CPT
- Include one page/post into query which is already returning posts
- Search for pages with permalink
- How to add a recent post function to a text widget I created for practice?
- Can I make a search query which includes a space?
- Help with if statement inside while loop
- wp-query problem with author
- WP_Query – display posts by custom field and order by another
- WP_Query no result if keyword contains number
- WP_QUERY order posts by two combined meta_value dates
- Custom WP Query order function possible?
- Order by value of Custom Field using url string
- How to set meta_query if get_post_meta returns nested array for that key? [duplicate]
- Is the ‘parse_query’ filter still valid to change filtered content on admin list view?
- I removed the “wp_rm_logs” table and am constantly getting this error
- How to set selected attribute on option after filter query?
- WP_Query returns empty if meta_query has more than 7 values
- Fetching $_POST from Page Template into functions.php
- query.php – multiple is_category functions
- Custom query with post_class filter using current_post not working
- Overwriting auto-appended NOT IN query in WP_Query
- posts_per_page not returning correct number of posts
- query_posts with sorting on a custom datestamp
- Conditionals in WP_Query
- Why WP_Query in functions.php is not working when get_posts works?
- Next / previous posts link doesn’t show up with Posts 2 Posts
- Loop the posts between two dates selected using jquery datepicker
- query by meta value then date and not empty meta value
- Issue with sorting by post date and custom post type
- Is it possible to use WP Query to orderby custom meta value and custom taxonomy?
- wordpress sorting using array merge by price in ascending order but price with 0 must be show last
- Remove divs and spans from post content
- Can’t get LIKE to work with wp_query [closed]
- Get all user with both meta_value
- Show number of posts AND number on current page (cannot make it work)
- Advanced Search – Is this possible?
- How to include custom fields in wordpress search