WordPress Theme Tutorial Pt 2

Wordpress Problems SolvedIn part 2 of my WordPress Video Tutorial I continue to teach you all that you need to know to create custom WordPress themes. If you haven’t seen part one check it out WordPress Theme Tutorial Part 1.

Here I teach you how to grab very specific information from your WordPress database. You’ll then be able to better organize everything on your site to create featured content slide shows and numerous other tools.

I’ll specifically show you how to:

  • Display Specific Posts Based off of Very Specific Attributes
  • Use All of the Features of the query_posts() Function
  • Use All of the Features of the get_posts() Function
  • Create a Highly Configurable wp_query Object

By the end of this tutorial you will know exactly how the WordPress Loop works.

All of the code used will follow the video. If you have any questions or comments leave them below.

Code From the Video

<!– Display only 2 posts

<?php query_posts(‘posts_per_page=2’); ?> –>


<!– Display only the post with the id of 46

<?php query_posts(‘p=46’); ?> –>


<!– Display only the post with the id of 46

<?php query_posts(‘page_id=46’); ?> –>


<!– Display only the pages on the site

<?php query_posts(‘post_type=page’); ?> –>


<!– Display a page by name

<?php query_posts(‘pagename=Sample Page’); ?> –>


<!– Display posts but skip the first 5

<?php query_posts(‘offset=5’); ?> –>


<!– Display posts with the category Recipes

<?php query_posts(‘category_name=Recipes’); ?> –>


<!– Display posts with the tag Eggplant

<?php query_posts(‘tag=Eggplant’); ?> –>


<!– Display posts by a specific author name

<?php query_posts(‘author_name=admin’); ?> –>


<!– Display posts by the month of creation

<?php query_posts(‘monthnum=3’); ?> –>


<!– Display posts by the year of creation

<?php query_posts(‘year=2011’); ?> –>


<!– Display posts by the day of creation

<?php query_posts(‘day=12’); ?> –>


<!– Display posts by title in descending order

<?php query_posts(‘orderby=title’); ?> –>


<!– Display posts by title in descending order

<?php query_posts(‘orderby=title&order=ASC’); ?> –>


<!– Display posts with a tag of Onion or Jalapeno

<?php query_posts(‘tag=Onion,Jalapeno’); ?> –>


<!– Display posts with a tag of Onion and Jalapeno

<?php query_posts(‘tag=Broccoli+Lemon’); ?> –>



<!– You can specify specific attributes that will effect the query results with the query_posts() function. –>

<?php query_posts(‘posts_per_page=5&orderby=title&order=ASC&paged=’.$paged); ?>



<!– You can also retrieve data from the database using get_posts(). Just issue the query for your custom list –>

<?php $postTitles = get_posts(‘posts_per_page=10&orderby=title&order=ASC’); ?>


<!– Cycle through the qualifying results with a foreach loop. Some post related data isn’t available when you use get_posts() so you have to make a call to setup_postdata() to resolve this issue –>

<?php foreach($postTitles as $post) :

setup_postdata($post); ?>


<a href=”<?php the_permalink() ?>”><?php the_title(); ?></a><br />


<?php endforeach; ?>




<!– You can only provide the newer entry and older entry links if you use the $wp_query variable. You can however create custom loops by storing the value of $wp_query in a temporary variable and then restoring that value after you are done using it –>


<!– Store the value of $wp_query in a temporary variable –>

<?php $temp = $wp_query; ?>

<?php $wp_query = null; ?>


<!– Create a new $wp_query object –>

<?php $wp_query = new WP_QUERY;?>


<!– Calls the custom query using the query() function. You must append the variable $paged so that the current page is passed. This allows you to use the newer and older entries links. –>

<?php $wp_query->query(‘posts_per_page=5&orderby=title&order=ASC&paged=’.$paged); ?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>


<a href=”<?php the_permalink() ?>”><?php the_title(); ?></a>

<?php the_content(); ?>

<?php endwhile; ?>


<div class=”navigation”>

<div class=”alignleft”><?php next_posts_link(__(‘&laquo; Older Entries’, ‘kubrick’)) ?></div>

<div class=”alignright”><?php previous_posts_link(__(‘Newer Entries &raquo;’, ‘kubrick’)) ?></div>



<!– Reset the value for $wp_query –>

<?php $wp_query = null; ?>

<?php $wp_query = $temp; ?>

5 Responses to “WordPress Theme Tutorial Pt 2”

  1. Ankur says:

    Hi Derek,


    Suggestion – You may want to use code formatting plugins for wordpress.

    Also how are you able to set that first image on your videos?

    • admin says:

      If you are referring to the YouTube thumbnail as the first image. I’m a YouTube partner. One of the perks of becoming a YouTube partner is that you can set your thumbnails among other things. I’ve tested all of the tricky ways to pick your thumbnail if you aren’t a partner and none of them seem to work.

  2. Derek-Great stuff! This is something that needs to be gone over and over and tested and tested. But it’s all here. There just doesn’t seem to be enough hours in the day. Gotta figure out how to stretch ’em somehow!



  3. B Dauria says:

    These tutorials are invaluable. Can’t thank you enough.

    Although I feel well behind… as these were created in 2011, I’m enjoying studying.

    Question: On the WP theme (Coraline) I chose, people can “register”, but I’ve no idea how to locate the names, and see their information. Through my “cpanel” I can see there are databases (maybe one or two are actually “holding” my WordPress blogs?) I’d like to know how to properly “query” so I can get “in” and see any registrants information. Do you have a tutorial for that?

    I apologize in advance if this is so fundamental that I should know it, but I’d appreciate any help.

    • Derek Banas says:

      Thank you 🙂 Actually WP hasn’t changed that much when it comes to designing themes and plugins. Everything is backwards compatible.

      You can find your users by logging into the Dashboard for your WordPress site. Look for users on the left hand side.

Leave a Reply to Robert Jackson Cancel reply

Your email address will not be published.