WordPress Custom Loop WP_Query Tutorial

Learn about WP_Query and create custom loops in any WordPress theme file. Check out my “Get a Web Developer Job” course:

To learn about everything that WP_Query is capable of check out the official WordPress documentation:

Link to download .zip of theme files as shown in this video (note: this is not a “complete” WordPress theme yet and this download is only intended for educational purposes to dissect and review):

To view a complete list of the WordPress lessons in sequential order visit:

Want to share the WordPress website that you’ve been creating on your computer with the world? Learn about the web host I use and how you can get a domain + hosting for $4.95 a month:

Sign up for my newsletter to receive periodic webDev tips, tricks, resources and coupons. Join the list at

Follow LearnWebCode on Twitter for resources and updates:






49 responses to “WordPress Custom Loop WP_Query Tutorial”

  1. jyoti barasker Avatar

    Sir you had not shared the video for creating content-single and content-page.php in your theme

  2. Yaroslav Soppa Avatar

    Thank you so much! +1 to Prasad comment. You are awesome, move forward man!

  3. Villa Almhill Avatar

    Neat lesson! Thanks!

  4. Roel Blomsma Avatar

    Thanks dude! Your videos are really helpful and didactical.

  5. Alston C Avatar

    Your commentary as you go through the code really is helpful. Looking forward to going through more of your videos. Thanks 🙂

  6. Ariunsanaa Avatar

    Hi everyone. I'm having a trouble to develop query that will bring the 2nd last post of the certain category.
    In that way, I'd like to style the last post in one way and the second last in another way.
    Any suggestion?
    Thank you 🙂

  7. dovahkiin Avatar

    how many wp_query in a single page? would this affect the performance?

  8. Romy Kim Avatar

    I've been trying to do this "from 2 separate category" thing and been very frustrated. Now I got it. Thanks.

  9. Majd Alyousfi Avatar

    Great tutorial Thanks

  10. Andy Funikov Avatar

    You are the best!
    Thanks for you tutorials! 🙂

  11. jellycoding Avatar

    Great tutorial. However I have a question. If I make a theme and give it to someone else. How do I know that the Opinion category is going to have the same ID in his installation?

  12. shadab hussain Avatar

    sir how to change wp_query post width in 3 to 4 column it displays only 100% size

  13. fayekhelmi Avatar

    I'm finding myself in a pickle… not sure why this is happening… i'm working on my homepage (front-page.php) i designed it to be a 3 block scrollereach block i show the last few posts from a category.

    I followed your video and i manage to get the outputs properly but i've noticed that the html page doesnt load the rest of the html after the loop!

    Not sure why that is….

  14. francesca fontana Avatar

    I so wanna say thank you!
    I knew literally NOTHING about html, php and css till a week ago and now i actually am able to do sooooo much!!
    Your explanations are perfect and these videos are just so well made.
    thank you again.

  15. kipupking Avatar

    I used your code, deleted the wp_reset_postdata(); and nothing changed, can you explain it briefly why and whats the purpose of it?

  16. Роман Иванов Avatar

    Love your videos! Thanks a lot!

  17. Saboj al-amin Avatar

    Sir My 2nd post is not showing

    <?php if ( have_posts() ) :

    while ( have_posts() ) : the_post();



    else :
    echo 'no content found';

    endif; ?>

    <div class="service_boxes">
    <div class="box1">


    // opinion posts loop begins here

    $opinionPosts = new WP_Query('cat=5&posts_per_page=2');
    if ($opinionPosts->have_posts()):

    while ($opinionPosts->have_posts()): $opinionPosts->the_post();?>

    <div class="topic_img"> <?php the_post_thumbnail(); ?></div>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <?php the_content(); ?>

    <?php endwhile;




    <div class="box1">


    // graphic posts loop begins here

    $graphicPosts = new WP_Query('cat=6&posts_per_page=2');
    if ($graphicPosts->have_posts()):

    while ($graphicPosts->have_posts()): $graphicPosts->the_post();?>

    <div class="topic_img"> <?php the_post_thumbnail(); ?></div>
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <?php the_content(); ?>

    <?php endwhile;





  18. Charles Loehle Avatar

    How can i get atom editor to do that thing where you type two matching lines of code at the same time?

  19. pratyush ranjan Avatar

    the tutorial is damn good but which editor you are using??it's also cool..

  20. Matias Valo Avatar

    Totally an Aha!-moment. Thank you, Sir!

  21. Recep İNANÇ Avatar

    You're just the best. Saving me. Thank you a lot!

  22. askas 04 Avatar

    thanks for tutorial before, but i have question , how you can make the button look like on the last minutes, can you make tutorial please??

  23. Der Rauber Avatar

    how embed post thumbnails (featured images) to index.php in underscores?
    (Underscores doesn’t embed post thumbnails by default to index.php in markup by php tags)

  24. Pedro Antunes Avatar

    Hey! I know this is an old video but I was wondering… could I create 2 different templates for those 2 columns and then call them with get_template_part? If I have my homepage structured with 6 rows, each with a different loop, wouldn that output a "cleaner" code? Is there any advantage or this just doesnt makes sense? Thanks!

  25. fahmi wira Avatar

    <!– post-thumbnail –>
    <div class="square-thumbnail">
    <a href="<?php the_permalink(); ?>"><?php
    if (has_post_thumbnail()) {
    } else { ?>

    <img src="<?php echo get_template_directory_uri(); ?>/images/leaf.jpg">

    <?php } ?></a>
    </div><!– /post-thumbnail –>


    please explain

  26. solomon jeeva Avatar


  27. PtxDK Avatar

    0:20 Just before I started the video i had this feeling. 😀

  28. Elton Sousa Avatar

    Really helpful tutorial and thanks a lot for it.
    How can I print the category name before printing the title of each post?

  29. Rajath kemal Avatar

    Hello, First of all i would like to thank you a lot for this wordpress theme development playlist. You deserve a good appreciation and respect for helping many beginner developers. I have developed a theme for my own sports website by learning from your tutorials. But i would like to suggest you to add videos on making the theme responsive and on adding page links(useful to see old posts when limited to 5 posts per each page). I have searched from various sources and completed my theme. I`m just suggesting so that it would be helpful for those who follow this playlist in future. Once again THANK YOU !!

  30. Md Masud Avatar

    It's amazing to me. I think i have learned so much things.

  31. Ameer Salah Avatar

    At the end of this tutorial, I just wanna say;
    Man you are awesome.

    Thank you very much,

  32. J4si3k1992 Avatar

    Nice tutorial, but I have one question. How to make one loop with custom post type (category) and normal category? How to make all in one loop?

  33. Allen Liu Avatar

    Nice text editor, good for identifying php code. Could u tell us the name?

  34. blowtar Avatar

    Is there a good guide you know of, that has all the main functions listed? Kinda like a cheat sheet.

  35. Vernon Joyce Avatar

    Just discovered these – they're great. Thanks for the effort.

    Will you by any chance at some point do a tutorial on creating your own widgets?

  36. Mohammed Adlan Avatar

    Great instructor
    thanx alot

  37. Muhammad Abrar Hassan Avatar

    Greatest among the teachers!!!

  38. Michael Oliver Avatar

    Guy is insane how much he knows!! Will I ever get THAT good!? Awesome stuff!!

Leave a Reply

Your email address will not be published. Required fields are marked *