Week 7 Post – Report 6

Accomplishment this Week

Reading: Professional WordPress, Chapter 5 Loop

After finishing the chapter about core files, I started learning about Loop in WordPress. Loop controls the content fetched from database and displayed on the page that is loading. If I want to display some content different from the default ones, for example, if I want to display posts alphabetically instead of chronologically,  I can use custom Loop to realize that.

The work flow of a default Loop is as follows:

  • If the URL matches some file on web server, the file will be directly loaded without WordPress getting involved.
  • Else, when the URL of the page is passed to WordPress, the parse_query() method of WP_Query Object will parse the URL into a sequence of query parameters.
  • Then get_posts() method of WP_Query Object will convert these query parameters into SQL query and run the query to fetch the post data from MySQL database. The raw post data fetched will be stored in a global variable $wp_query of the WP_Query Object.
  • After getting the data, WordPress set all conditional tags like is_home and is_single.
  • Then WordPress chooses a template file within the theme based on the Template Hierarchy.
  • The code inside template files will display the posts. The skeleton is shown in the picture below:minimum loop
    • In every iteration, the_post() method in turn calls setup_postdata() method which sets up the global variable $post. The post-related information of this particular post stored in this variable is not raw data, but filtered content (related to plugin). After the Loop, $post variable stores the data of the last post displayed.
    • The loop content will be displayed using template tags, which are PHP functions used to display post-related content. For example, the_title(), the_content() and the_excerpt(). Many template tags have parameters like the_title(’<h1>’, ‘</h1>’) which takes two parameters $before and $after.

Customization of Loop

If I want to customize the Loop, I can build my new WP_Query Object or use lower-level functions query_posts() and get_posts().  Among these, creating a new WP_Query Object is the referred way.

  • New WP_Query Object

Creating a new WP_Query Object and making query with parameters/arguments will return the posts desired. (key here is to choose appropriate arguments)

This is similar to the default query. The difference is that the arguments are not from URL but given explicitly, and the methods have_posts() and the_post() are called from newly created WP_Query Object instead of the global variable $wp_query.

$myPosts = new WP_Query(); 
$myPosts->query(’posts_ per_ page=5’);  
if($myPosts->have_ posts()):
while ($myPosts->have_ posts()) : $myPosts->the_ post(); ?> 
<!-- do something --> 
<?php endwhile; 
end if;?>

three custom Loop ways$myPosts->query() calls get_posts() method of WP_Query Object to get posts and set flags like is_home and is_single. Template file within the theme will be chosen to display the content based
on Template Hierarchy. (same to default Loop)

  • query_posts()

query_posts($arg) function should be placed right before the start of Loop, which is if(have_posts()). It explicitly overwrite the content returned from the default Loop. It should not be used more than once and should not be used inside the Loop. It may result in wrong conditional tags because tags are set during the default Loop which has different return content from the custom Loop. It also slows down the loading because it executes another query. wp_reset_query() function should be added manually after the Loop to restore the original query.

  • get_posts()

get_posts($arg) function returns an array of raw post data and needs to manually add setup_postdata() function in order to use template tags.


Liver sever

Cynthia and I have set up the live server this morning.
We use a computer in IEM Lab as our server (downloaded Bitnami on it) and we are granted an administrative account to it. If we want, we can go to the lab and log on to the computer.

Visit the site: For anyone who is outside the NTU intranet, he/she can use NTU VPN first and go to the address ( is the IP address of our server) to visit the WordPress home page. Cynthia and I can login at to access the dashboard.

liver server visit
Visit Live Server
liver server dashboard
Access Dashboard as Logged-in User
Synchronization: Prof Chua recommends another way of transferring files which is using remote control. While using the server IP address and the administrative account, we can use NTU VPN first and then remotely control the server computer. Then the transferring is simply copy and paste. I have copied my theme and some plugins to the WordPress on live server.
Remote Control of Live Server
Remote Control of Live Server

Now we have setup one WordPress on this server. If we want to have two WordPress we need to assign them to different port numbers. We will do that next week.


Planning for the next week

Next week we will finish the 2-week project. We planned to start coding on this Wednesday but were postponed. We will start doing that after Beverley give us her design and we will push the result onto the live server when finished.

I will continue reading the book Professional WordPress as scheduled.


5 comments on “Week 7 Post – Report 6

  1. Great to see you have the live server setup! I am curious though if you need to login to the NTU VPU just to view the site? I hope the viewing is public even if the login requires VPN. I suppose this is a way to keep things more secure. Your WordPress report is very interesting, if not a little beyond my expertise, you have definitely passed me up! But great to see you showing such a thorough understanding of the WordPress Loop, which is at the heart of how content is displayed.

      1. Thanks. Re-organize the things I read and write them down can help me better understand them. I really want to understand Loop thoroughly and I am sure I will need to customize them for my later theme development.
        For the links, thanks for your remind, I simply forgot to add hyperlinks. I edited them now, so after you install and run the VPN, you should be able to go to our live server page by clicking it.
        Enjoy your weekend!

Leave a Reply