The loop in WordPress refers to what content to display on a page you are visiting. It can be posts, pages or custom content. A loop can display single piece of content or a group of posts and pages that are selected and then displayed by looping through the content. The loop selects posts from the MySQL database based on a set of parameters. Those parameters are generally determined by the URL that’s used to access the website. Understanding how loop functions help you understand how can you control it.
To understand the loops let’s break down how WordPress generates a page’s content.
- The URL is matched against existing files and directories in WordPress installation. If the file is there it’s loaded by web server.
- If there’s no matching file or directory in the WordPress installation for the URL, then web server starts by loading the WordPress core by index.php. It will start the loop.
- WordPress parses URL into a set of query parameters. Based on those query parameters WordPress determines what content to display.
- WordPress conversts query specific parameters into a MySQL database query to retrieve the content. The content retrieved from MySQL database is then saved in the WP_Query object to be used in the WordPress loop.
- Once the content is retrieved WordPress sets all of the is_conditional tags such as is_home() and is_page() which helps to display posts based on the type of the page.
- WordPress picks a template from your theme based on type of query and number of posts returned. And output is presented to the browser.
This loop can be customized for different website purposes.
Understanding Content in WordPress
By default WordPress defines two types of content, posts and pages. All content types are stored in same table in MySQL table , differentiated by their content type. It is possible to have our own content types too. Most important type of post in WordPress is posts. Loop controls how your content is displayed. It’s a connection between your database and final HTML that’s rendered on the browser. Most WordPress theme contain a header, sidebar, content area and footer. Most often loop places content in the content area of WordPress website.
Loop checks whether any posts or content exists and if exists while statement is used to initiate the loop and cycle through all posts and pages that need to be displayed. The function the_post() is called to build post data and can be displayed in the content area.
Following is a simple example of loop with minimum code.
if ( have_posts() ) :
while ( have_posts() ) :
//loop content (template tags, html, etc)
WordPress stores user edited content exactly as it is entered, so if a user adds a shortcode it is also stored in the database. When the post setup is done while retrieving, the registered plugin that converts shortcode into post content by applying filters. The loop is usually surrounded HTML tags which gets displayed on the page.
Customizing the way post content is displayed depends on post metadata, these are called template tags. These template tags are used to display specific pieces of data about your website and content. This allows us to customize how post is displayed. Following are the common template tags used in displaying post.
- the_permalink() – Displays the URL of the post
- the_title() – Displays the title of the post
- the_ID() – Displays the ID of the post
- the_content() – Displays full content of your post
- the_excerpt() – Displays the excerpt of your post.
- the_time() – Displays the date/time post was published
- the_author() – Displays the author of the post
- the_tags() – Displays the tags attached to the post
- the_category() – Displays categories assigned to the post
- edit_post_link() – Displays an edit link only if you are logged in and allowed to edit
- comment_form() – Displays complete commenting form for post
Each template tag have parameters. You can look at those parameters or inspect the template tag functions just go and check /wp-includes/post-template.php
Customizing the Loop
WP_Query is a class that is defined in WordPress that makes it easier for you to create custom loops. When you are using query_posts() the global $wp_query an instance of WP_Query is used as default data store. Custom loops are built using separate instances of WP_Query class. We need to pass parameter string to instance of WP_Query objects to get particular content from the database . The following code is an example for that
$myPosts = new WP_Query( 'posts_per_page=5' );while ( $myPosts-
<!–– do something ––>
<?php endwhile; ?>
Here the parameter is posts_per_page . In the same way number of parameters can be inserted into WP_Query objects to retrieve different set of posts. Following are some of the post parameters . Following are some of the commonly used post queries based on their category
- Post Parameters – p, name, post_status, ignore_sticky_posts, post_type, posts_per_page, offset.
- Page parameters – page_id, pagename
- Category Parameters – cat, category_name
- Tag parameters – tag, tag_id
- Author Parameters – author, author_name, author_in, author_not_in
- Date and Time Parameters – monthnum, day, year
- Custom Field Parameters – meta_key, meta_value, meta_query
- Ordering Parameters – orderby, order
Below are some of the examples of retrieving posts with a query string
Display all posts with the bacon tag, excluding posts in category ID 5:
$myPosts = new WP_Query( 'cat=-5&tag=bacon' );
Display all posts with the tag writing or reading :
$myPosts = new WP_Query( 'tag=writing,reading' );
Display all posts with the tags writing and reading and tv :
$myPosts = new WP_Query( 'tag=writing+reading+tv' );
Display all posts with a custom fi eld named color with a value of blue :
$myPosts = new WP_Query( 'meta_key=color&meta_value=blue' );
We can use ‘pre_get_posts’ hook to alter query string before sending any query to database.
query_posts() function is used to easily modify the content returned for default WordPress loop.
get_posts() retrieves raw post data , however it lacks template tags needed to display in the theme. Those template tags can be setup using setup_postdata($post) function.
wp_reset_data() restores global $post variable to the current post in the main query
wp_reset_query() restores the original query , including the conditional tags setup early in the URL parsing process.
The loop can be used multiple times throughout your theme and plugins. This makes it easy to display different types of content in multiple places throughout WordPress website. The loops can be nested. Loops can be executed within a loop.
There are some global variables that stores information about content, author etc , which can be accessed anywhere within WordPress execution environment. Post data is stored in $post, author data is stored in $authordata, $current_user stores data about currently logged in user. There are some environment variables like $is_IE, $is_safari, wp_is_mobile() , that stores about browser details or whether browser is in mobile or desktop etc. There are some functions that operate on sets of posts. The following functions work outside the loop
wp_list_pages() – Displays a list of pages as links
wp_list_categories() – Displays a list of categories as links
wp_tag_cloud() – Displays a tag cloud from all tags
get_permalink() – Returns the permanent link of a post
next_posts_link() – Link to next post
previous_posts_link() – Link to display previous posts
get_post() – Returns post with specific ID.
the_author_meta() – This retrieves information about author.
These are the most important functions that help us to display content as we wish in a WordPress site.