WordPress $GLOBALS :: 2018-02-28 19:13:09

WordPress $GLOBALS

how WordPress $GLOBALS fit in to the looping mechanism (or don’t)


Speedway Fight Club

[excerpt]stanWordPress $GLOBALS, why we need to know about them, and understanding there scope & usage

When you use the_title() or the_content() or the_author() or any of the very many template functions, WordPress goes looking for this information in the $post global. As a general rule, when working in the template files, you will always want to use these template functions. To understand why globals are needed or not, think about the “current item” or “context”. When a Loop runs the_post(), it is setting up the globals within the current item context.

To access a global variable in your code, you first need to globalize the variable with

global $variable;

I like to get the information straight from the source (codex.wordpress.org) when possible, the following data comes from https://codex.wordpress.org/Global_Variables

Inside the Loop variables

While inside the loop, these globals are set, containing information about the current post being processed.

$post (WP_Post)
The post object for the current post. Object described in https://codex.wordpress.org/Class_Reference/WP_Post
Class Reference/WP Post">Class_Reference/WP_Post
$authordata ( WP_User ) /**
The author object for the current post. Object described in "https://codex.wordpress.org/Class_Reference/WP_User"
$currentday ( string ) /**
Day that the current post was published
$currentmonth ( string ) /**
Month that the curent post was published
$page ( int )
The page of the current post being viewed. Specified by the query var page.
$pages ( array )
The content of the pages of the current post. Each page elements contains part of the content separated by the <!--nextpage--> tag.
$multipage ( boolean )
Flag to know if the current post has multiple pages or not. Returns true if the post has multiple pages, related to $pages.
$more ( boolean )
Flag to know if WordPress should enforce the <!--more--> tag for the current post. WordPress will not enforce the more tag if true.
$numpages ( int )
Returns the number of pages in the post, related to $pages.

setup_postdata() sets up global post data and fills the global variables $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages, which allows Template Tags access to data in the current post context.

  • does not assign the global $post variable so it’s important that you do this yourself.
  • Now we know what we are working with when we run a “default” query

    here is an example of the globals that are constructed on Speedway Fight Club’s home page

    <!-- GLOBALS--><!-- NOUVEAU--><!-- PHP_SELF--><!-- _COOKIE--><!-- _ENV--><!-- _FILES--><!-- _GET--><!-- _POST--><!-- _REQUEST--><!-- _SERVER-->
    <!-- _wp_additional_image_sizes--><!-- _wp_admin_css_colors--><!-- _wp_deprecated_widgets_callbacks--><!-- _wp_post_type_features--><!-- _wp_registered_nav_menus--><!-- _wp_sidebars_widgets--><!-- _wp_theme_features--><!-- _wp_using_ext_object_cache-->
    <!-- akismet_api_host--><!-- akismet_api_port--><!-- allowedentitynames--><!-- allowedposttags--><!-- allowedtags-->
    <!-- attachment--><!-- attachment_id-->
    <!-- author--><!-- author__in--><!-- author__not_in--><!-- author_name--><!-- authordata-->
    <!-- blog_id--><!-- cache_results-->
    <!-- cat--><!-- category__and--><!-- category__in--><!-- category__not_in--><!-- category_name-->
    <!-- comment--><!-- comments_per_page-->
    <!-- compress_css--><!-- compress_scripts--><!-- concatenate_scripts-->
    <!-- content_width-->
    <!-- year --><!-- currentday--><!-- currentmonth--><!-- month--><!-- month_abbrev--><!-- monthnum--><!-- weekday--><!-- weekday_abbrev--><!-- weekday_initial--><!-- day--><!-- hour--><!-- minute--><!-- second-->
    <!-- custom_background--><!-- custom_image_header-->
    <!-- debug_bar-->
    <!-- embed-->
    <!-- error-->
    <!-- feed-->
    <!-- fields-->
    <!-- id-->
    <!-- ignore_sticky_posts-->
    <!-- is_IE--><!-- is_IIS--><!-- is_NS4--><!-- is_apache--><!-- is_chrome--><!-- is_edge--><!-- is_gecko--><!-- is_iis7--><!-- is_iphone--><!-- is_lynx--><!-- is_macIE--><!-- is_nginx--><!-- is_opera--><!-- is_safari--><!-- is_winIE-->
    <!-- l10n--><!-- l10n_unloaded-->
    <!-- lazy_load_term_meta-->
    <!-- locale--><!-- m--><!-- menu_order-->
    <!-- meta_key--><!-- meta_value-->
    <!-- mivhak_options-->
    <!-- more-->
    <!-- multipage--><!-- name--><!-- no_found_rows-->
    <!-- nopaging--><!-- numpages--><!-- order--><!-- p--><!-- page--><!-- page_id--><!-- paged--><!-- pagename--><!-- pagenow--><!-- pages-->
    <!-- pass_allowed_html--><!-- pass_allowed_protocols-->
    <!-- post--><!-- post__in--><!-- post__not_in--><!-- post_name__in--><!-- post_parent--><!-- post_parent__in--><!-- post_parent__not_in--><!-- post_type--><!-- post_type_meta_caps--><!-- posts--><!-- posts_per_page--><!-- subpost--><!-- subpost_id-->
    <!-- preview-->
    <!-- query_string--><!-- request--><!-- required_mysql_version--><!-- required_php_version-->
    <!-- s-->
    <!-- sentence-->
    <!-- shortcode_tags-->
    <!-- show_admin_bar--><!-- sidebars_widgets--><!-- static-->
    <!-- suppress_filters-->
    <!-- table_prefix-->
    <!-- tag--><!-- tag__and--><!-- tag__in--><!-- tag__not_in--><!-- tag_id--><!-- tag_slug__and--><!-- tag_slug__in--><!-- tb-->
    <!-- template--><!-- theme_root-->
    <!-- timestart-->
    <!-- tinymce_version-->
    <!-- title-->
    <!-- update_post_meta_cache--><!-- update_post_term_cache-->
    <!-- current_user--><!-- user_ID--><!-- user_email--><!-- user_identity--><!-- user_level--><!-- user_login--><!-- user_url--><!-- userdata-->
    <!-- w-->
    <!-- wp-->
    <!-- wp_actions-->
    <!-- wp_admin_bar-->
    <!-- wp_cockneyreplace--><!-- wp_current_filter--><!-- wp_customize--><!-- wp_db_version--><!-- wp_did_header--><!-- wp_embed--><!-- wp_file_descriptions--><!-- wp_filter--><!-- wp_header_to_desc--><!-- wp_json_auth_cookie--><!-- wp_local_package--><!-- wp_locale--><!-- wp_locale_switcher--><!-- wp_object_cache--><!-- wp_plugin_paths--><!-- wp_post_statuses--><!-- wp_post_types--><!-- wp_query--><!-- wp_registered_sidebars--><!-- wp_registered_widget_controls--><!-- wp_registered_widget_updates--><!-- wp_registered_widgets--><!--wp_rest_auth_cookie--><!-- wp_rewrite--><!-- wp_roles-->
    <!-- wp_scripts-->
    <!-- wp_smiliessearch-->
    <!-- wp_styles-->
    <!-- wp_taxonomies-->
    <!-- wp_the_query-->
    <!-- wp_theme_directories-->
    <!-- wp_user_roles-->
    <!-- wp_version-->
    <!-- wp_widget_factory-->
    <!-- wpcom_api_key-->
    <!-- wpdb-->
    <!-- wpsmiliestrans-->

    « »