Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress From Bootstrap to WordPress Create Bootstrap Styled Theme Templates Creating a Portfolio Landing Page

Lucas Santos
Lucas Santos
19,315 Points

Please explain this section of wordpress code

Hi im not really understanding this part of code to display the portfolio page

<?php
      $args = array(
      'post_type' => 'portfolio'
      );
      $the_query = new WP_Query( $args );

      ?>

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

I know that $args is an array but now the key and the value of that array is what im not understanding. why 'post_type' and why 'portfolio' is portfolio a specific id set to the portfolio field we created or is it just a random variable?

and what is $the_query = new WP Query ( $args ); exactly?

I understand how the loop works and all but just not what it's passing on top such as the_query.

1 Answer

Andrew McCormick
Andrew McCormick
17,730 Points

'portfolio' would be the custom post type that you are querying. So in this case you are starting a new instance of the WP_Query class, and you are querying for any posts that are of the custom post type called 'portfolio'.

There are many arguments that you can pass to WP Query:
WP Query - Type Parameters

Lucas Santos
Lucas Santos
19,315 Points

Just a couple questions because im not fully understanding,

  1. Where do I set the post type to portfolio, in the admin area under the custom fields section?

  2. What is the_query? a pre set value of some sort or just a random variable

  3. And if im understanding correct WP_Query is a wordpress class that displays different parameters??

Andrew McCormick
Andrew McCormick
17,730 Points

Where do I set the post type to portfolio if you are following the video you set it using the 'Custom Post Type' plugin.
What is the_query? yes. it's just a variable name that makes sense. you could use any valid variable name here.
And if im understanding correct WP_Query is a wordpress class that displays different parameters?? It defines your loop. From the codex: WP_Query is a class defined in wp-includes/query.php that deals with the intricacies of a posts (or pages) request to a WordPress blog. The wp-blog-header.php (or the WP class in Version 2.0) gives the $wp_query object information defining the current request, and then $wp_query determines what type of query it's dealing with (possibly a category archive, dated archive, feed, or search), and fetches the requested posts. It retains a lot of information on the request, which can be pulled at a later date. If you already understand the Loop then take some time to read the codex on [WP Query}(http://codex.wordpress.org/Class_Reference/WP_Query) next.

Lucas Santos
Lucas Santos
19,315 Points

I see ok thanks for clearing that up! appreciate it