Maybe I'm going about this the wrong way but this is my scenario... I would like to use WordPress as a CMS rather than a blogging platform. I have a website that will have somewhere between 25-50 pages and will not have any posts.

I'm looking to develop my own theme which should be fairly easy, however I'm getting a little confused on using The Loop functions.

I understand that:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

is used for a page that is pulling x number of posts, however in my scenario, I only want to pull the contents of a page rather than x number of posts. Do I need to create custom SQL queries?

All of the examples I keep running into are using The Loop for posts. If I'm just using static pages, do I even need to use The Loop? Can I just use the unique URL to pull a page out of the post column of the MySQL database?

I hope this is making sense.

2 Answers 2


Strictly, you don't need to use a loop in a page template, but it doesn't hurt, the content of the page will still load, the loop will simply only run once as there is only one post/page. Many themes include a loop in page templates, I guess for some compability issue.

If you are building your page template from scratch, you don't need to add a loop to it, you are right, wordpress will get the right content from the URL, but you still need to run the function the_post() before to use template tags like the_title(), the_content(), etc. Just put the the_post() towards the top of the template.

Hope it makes sense to you, don't hesitate to ask if you need further clarifications.

  • Agreed. The loop doesn't hurt and only runs one time on a page anyway. Commented Jun 25, 2014 at 17:12
  • while you may often experience no side-effects, you can potentially break things by not calling have_posts(). when the end of the loop is reached, the last call to have_posts() triggers the loop_end action. anything hooked to that action won't run if you don't use the full loop code.
    – Milo
    Commented Jun 25, 2014 at 17:42
  • Thanks for the feedback! I guess I'm still a little confused though. How does WP associate the page, www.example.com/learn/, with the page content stored in the database, SELECT * FROM wp_post WHERE post_id = ??? How does it know, or how do I tell it to retreive the /learn/ page where I've created the HTML?
    – markmilly
    Commented Jun 25, 2014 at 18:50
  • It works with rewrite URLs, that's what you set up on the permalinks settings. What the browser shows, /learn/, gets translated internally to index.php?page=12, etc. All this is abstracted from you, you don't need to do anything to make it work. Commented Jun 25, 2014 at 21:01


Pages, like posts, are stored in the database as post_types, the big difference here is that pages lives outside the normal time-based listing. Check at what the codex say

What Pages Are:

  • Pages are for content that is less time-dependent than Posts.
  • Pages can be organized into pages and subpages.
  • Pages can use different Page Templates which can include Template Files, Template Tags and other PHP code.
  • Pages may have a more complex array of readily available display adjustments when using sophisticated Themes with extensive customization.
  • In essence, Pages are for non-blog content. It is possible to remove all or most Posts from a WordPress installation, and thus to create a standard non-blog website.

What Pages are Not:

  • Pages are not Posts, nor are they excerpted from larger works of fiction. They do not cycle through your blog's main page. WordPress Plugins are available to change the defaults if necessary.
  • Pages cannot be associated with Categories and cannot be assigned Tags. The organizational structure for Pages comes only from their hierarchical interrelationships, and not from Tags or Categories.
  • Pages are not files. They are stored in your database just like Posts are. Although you can put Template Tags and PHP code into a Page Template file, you cannot put these into the Page or Post content without a WordPress Plugin like Exec-PHP which overwrites the code filtering process.
  • Pages are not included in your site's feed.
  • Pages and Posts may attract attention in different ways from humans or search engines.
  • Pages (or a specific post) can be set as a static front page if desired with a separate - - Page set for the latest blog posts, typically named "blog."

Pages are mainly used for non-blogging purposes, and will suite your needs perfectly. You can have a look at how to create page templates in the codex.


To come to your question, all information entered into a page is stored in the database, just as posts are, but unlike posts which is stored as post_type post, pages are stored as post_type page. To get that and display these information, you have to make a query to the database to call that information.

Here is how the main query determine what information to return when a page loads, Taken from Query Overview

So, here are the steps WordPress uses to decide what posts or pages to display on a page, and display them:

  • When a visitor first clicks on or types a URL for a page that is part of your blog, WordPress starts by running a few core files (wp-config.php, wp-settings.php, etc.) If you are interested in the specifics of the file loading order, start at index.php and follow the chain of files as each PHP file includes/requires additional PHP files (or read this excellent post at humanshell.net)
  • WordPress loads and initializes any plugins you have activated (calls the plugin init actions).
  • WordPress loads the "text domain" for internationalization, and the functions.php file from the currently active theme.
  • WordPress runs the wp() function (in wp-includes/functions.php), which calls $wp->main() ($wp is an object of class WP, which is defined in wp-includes/class-wp.php). This tells WordPress to: -Parse the URL into a query specification using WP->parse_request() -- more on that below.
  • Set all the is_ variables that are used by Conditional Tags using $wp_query->parse_query() ($wp_query is an object of class WP_Query, which is defined in wp-includes/query.php). Note that in spite of this function's name, in this case WP_Query->parse_query doesn't actually do any parsing for us, since that is done before-hand by WP->parse_request().
  • Convert the query specification into a MySQL database query, and run the database query to get the list of posts, in function WP_Query->get_posts(). Save the posts in the $wp_query object to be used in the WordPress Loop.
  • Handle 404 errors.
  • Send the blog's HTTP headers.
  • Set up some variables for the WordPress Loop.
  • WordPress loads your template, figures out which template file to use according to the Template Hierarchy, and runs that file (basically doing whatever your template file says to do). Or, WordPress could run one of the feed files (such as wp-rss2.php) instead. Generally, the template or feed file runs the WordPress Loop to print blog posts or a static page.
  • The template or feed file will also likely print out permalinks to some archives, categories, or posts using built-in WordPress functions.

I understand that:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

is used for a page that is pulling x number of posts, however in my scenario, I only want to pull the contents of a page rather than x number of posts

So the answer to this question is, the loop only output the information/data from the main query. It is the main query that retrieves the data from the database, and as said before, the main query retrieves only the information according to URL and post type

Do I need to create custom SQL queries?

No, the main query has alreaady done that. You can just use the_post() to get that information from the main query(). If you need to make custom queries, rather use WP_Query. Never use your own SQL queries

If I'm just using static pages, do I even need to use The Loop?

It is not necessary, you can just simply use the_post() to display your information. It is however good practice to use the loop, as have_posts() triggers the loop end when the loop reaches the end.

I hope this explains as much issues you might have. If I can leave you with this, never try to reinvent the wheel, but that doesn't mean you cannot improve on the design if that day comes. But for now, stick to the basics, and make use of the default loop almost always.

  • the loop doesn't do any querying, it just outputs the contents of a query object. if you don't run the loop, the main query will still run, and you could actually build a query object yourself without querying the database at all, and the loop will output it.
    – Milo
    Commented Jun 25, 2014 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.