WordPress Custom Query – Part 01 – Basics
Video Title: WordPress Custom Query – Part 01 – Basics
Hello this is even for watch alarm calm and this series we are going to be talking about WordPress custom queries as you will see you will press custom queries are what makes difference in WordPress so a lot of people like to say well WordPress is just a blogging
Platform if take a look at these videos you will see that WordPress is not just a blogging platform and if you master a WordPress custom queries you can do pretty much anything you want with WordPress so what those queries actually are well WordPress custom queries are
Are a way for you to display the data on your site in whatever way you want as you will see we are going to be doing some basic basic examples of that and then we are going to move to more advanced examples and I’m going to show
You how you can use a how you can do something like custom searches making filters for your site and so on WordPress Karthik where is a very powerful powerful thing if you’ve been watching my Drupal series there are something like views for WordPress but you do them exclusively exclusively in
PHP you don’t have a UI for them and I would even say that just because of that they’re a bit more flexible what you will be needing for this series is fresh or even not so fresh installation of WordPress WordPress comes with custom queries by default of course also you
Can install this faker press plugins so you can generate some content for your site I generate some posts some categories and some tags of course you will be needing the theme you can use whatever default theme comes with WordPress you can use your own theme you can use Olympus theme that I’m using
Right here it has just a little bit of styling so that it doesn’t look hideous and one last thing I wanna mention is that everything that I’m doing here is going to be available for you on github so if you go this is one of my previous
Series so one page up with Drupal and view so for every episode you have if you go to releases right here you have the source code that you can download if you don’t want to write your own code and you can just copy and paste it into
Your project and I think that’s about it for series introduction right now in this episode we are going to I’m going to show you what custom queries are how they are different from the standard WordPress loop and we are going to do some basic examples of custom queries so
Right here I have some posts and this is the front page of my site so let’s say I want to display some of those posts on my front page I would go to my theme which is called Olympus I would go to index dot PHP which is the first file
That’s going to be loaded if you go to the URL of your WordPress installation and then you would do something like this so this is the standard the way you would do this you would do this standard WordPress loop also called the loop so if have posts will have post
The post and this is Croatian for content here so you will be learning some Croatian in this episode okay so if we have post well have post the post it’s pretty self-explanatory and then you can display the fields that the WordPress uses to display posts so let’s say we want to
Display the titles so these are titles we would you do just something like this PHP the title and that’s it I’m just going to add a br tag right here so that we have every title in the separate line save it go to our site and as you can
See we have those posts right here I don’t know why there are not right here let me just see oh so they are not in the container I’m sorry I should have put them in this container div so if we save this now they are here so we were
Displaying some posts right now we’re displaying 10 posts okay so what if your client comes to you and says I want to display 20 posts on my front page well is enough you will just go to settings reading and then you would 220 right here Save Changes and now you would have
20 posts and that’s about it with what you can do with that standard loop of course 2000 in your archive pages on your category pages on your user pages wherever you go in WordPress there will be 20 posts so that is the problem if your client comes to you well I want to
Have I don’t know 5 posts on the front page and I want to have 20 post on my category page and I want to have display for users on my your author pages and so on so that’s a bit of a problem and as you can see these are the only options
That you have in WordPress for selling rap now comes the wordpress custom query so let’s say our client says to us well I want you to display just five posts on the front page you can of course go right here and change that or you can go
To your index dot PHP delete this and then you set up your custom query for displaying five posts so how would you do that you will just do PHP and then you have to set up some arguments so WordPress custom query or WP query object has a lot of arguments and I will
Leave this link for you in the description below because it lists all the arguments that you can use in WP query as you can see there go from simple ones to something like this while doing meta queries and so on which we will be covering in this series that are
A little bit more complicated so this is the list from Bill Erickson and I use this page quite often when I want to quickly see what all of these arguments do so we have to set up some arguments we’re not going to be using this page in
This episode because we are going to be doing some basic stuff but let’s set up some arguments so we do args of course you can call this variable whatever you want you can call it conditions or whatever you want and you just do equals array and then you set up
Array of values so array of conditions that that type of data should be displayed as so first of all what do we want to display we want to display posts is enough you just do a post type post type you don’t do equals but you do this
Error right here and you say the post type should be post so you can help many post type post types especially if you’re using custom post types so you can have two three five of custom post types and you can display them all with WordPress custom queries okay but since
We only have posts right now we are going to display post and then well out our client said I want you to display only five posts is enough you just say posts per page it’s in English so post per page is going to be five and that’s
It you set up your arguments now you have to create a new WP query object and we are going to call that object just query okay query equals two new WP query and that a new object needs to receive some arguments to display that the data we define those arguments right here so
We would just do arcs and that’s it now you have to make your loop this loop is pretty similar to the standard WordPress loop so you would do something like while so while we while this query has posts so have posts then the display that post
For that query of course query the post and that’s it now you have your loop and we have to close that loop somehow so we would close it with of course since we have wild right here we would close it with end while and then we can we have to do
Something else so whenever you open up a new query and if you have a query down below on your page you have to reset the query so so it doesn’t screw this up the next where you would have on that page or even standard WordPress loop so you
Would do WP reset query and that’s it so WP reset query and now we’re done all that left to do is we have to display something in that loop so just like the previous example let’s just display the titles of our query so I’m just going to
Do h5 right here and the PHP the title save it and if we go right here now we have just five ports if I wanted to show seven posts I would do just this and now we have seven posts if I want to show
One post I will have one post as you can see this is pretty easy so you just define your arguments the define your new w WP query object paste some arguments right here and then you create your loop and start displaying the data on your site okay so let’s do a few more
Examples before we finish with this video so let’s say we are going to have ten posts right here okay now we have ten posts but your client comes to you and says okay so you’re now displaying those posts from the newest to the oldest I want you to display them from
The oldest to the newest is enough you would just go to order order and you said say descending so it’s going to automatically display your posts by date we will learn later how you can change that but for now you just do the ESC descending save them save it I’m sorry
It should be ascending ASC and now we’re displaying our post from the oldest which is this one to the newest which is this one if we were displaying ten posts okay so let’s say our user wants our client actually wants for you to display a featured post just
One featured post and then he wants all of the other posts to beat this plane differently so we can do something like this we are going to display just one post put it in a div called featured so we have it in a difficult featured
I’m just going to open my CSS and I’m going to say featured is going to be he’s going to have pairing of 20 pixels and background of ef ef ef okay and that should be it so if we save it right now we should have one post and it’s going to be in
This block right here I’m just going to change the color of that block to be something like red just so we can see it better it doesn’t look so good but we can see it better okay so our user wants this supposed to be featured so displayed like this but let’s say he
Wants another five posts displayed a different way so what you can do is you can just go and create a new query I’m just going to copy all of this out so we are going to create a new query it’s also going to be a calling post but it’s
Going to be displaying five posts and we are going to remove this feature right here and then I’m just going to set this to be BTech okay save this and now we have our featured post and we have five of our not so featured posts what else can we
Do because the client will look at the site but okay we have hello world featured post right here but why is it showing up right here also okay so you would say to him so we are not going to display in this list we are not going to
Display this hello world post so static way of doing this would be if you go to hello world and check the post ID so you can check it down here when I scroll over with the mouse and it says post equals one so does the idea of the post
So what you can do you can go right here and say post not in you have to do two underscores right here so post not in and then this is a little bit different so you wouldn’t give it just the ID of the post but you
Have to create an array so we do array and then you can put as many posts in here as you want so we want the post with the idea of one not to show in this query so if we save this go to our page
As you can see we have one two three four five posts but the hello world is not showing up of course you wouldn’t do this the static way you would do it the dynamic way so what you can do you can just go right here and you can create a
Variable called don’t show this guy of course you wouldn’t call like this but and you would do post ID so don’t show this guy variable is going to be one if we do echo right here it should be one as you can see so this
Is the idea of this hello world post so we don’t want to do it statically we don’t want to have to hard code that idea right here instead we create a variable which is going to carry our post ID and then we just put it right
Here so don’t show this guy and that’s it we can delete this and we get the same result so we don’t have hello world right here but we are displaying five ports okay so I think that’s about it for the first episode as you can see WordPress custom queries
Can be super powerful you can do pretty much anything with them and you can make your site to be as complex as you need to be if you understand and master WP query object and WordPress custom queries so thank you for watching this episode if you liked it please like it
If you like the channel please subscribe to it you can follow me on Twitter or on Facebook remember all the code that we used right here will be available for you in the on the gift hub repository link will be in the description below
And that’s it I will see you in the next episode
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Hey. Great tutorial. What theme do you use for your code editor? Thanks
sir is it possible to search the date? Thanks
Your videos are absolutely amazing I was looking forward for this kind of videos no one is making this kind of content oh my god these meta queries custom queries have made my day make more videos like these explaining woocommerce and do you have a Patreon so people can donate you ?
Thank you so much. You passed the knowledge clearly, directly and simply. A simply wonderful class.
here you're only displaying the titles, but how can I display both the title, the featured image, and the content of the post? thanks an advance
perfect
Pozdrav Ivane. Je li ovo i dalje relevantno?
svaka cast, Ivke!
Ivan, thank you for all the value you share. While this series is pushing 5 years old, it still holds a ton of good info. So much so it inspired me to write a plugin where you can access WP_Query from Javascript. Keep up the good work!
This is the best tut i've seen, thank you so much!
Thank for your turtorial Bro
Great Great Great
great, you helped me
Dear Sir, I greatly appreciate the time and effort you put into this video (episode 1). You are very clear and patient. I look forward to viewing all of the episodes regarding WP_Query. Thank you.
Thank you so much.
That's what i've been searching for! Thanks a lot <3
Ай го ту скул братан
Nice videos dude , this is the heart of WordPress ! Thank you , can i ask how have you saved on your editor some functions like ( the loop ) and it suggest you automatically when you start typing ? This will help a lot with my workflow !
I love the way you explain wordpress query.
My English is not Good, but I understand you better than others teachers in spanish. ¿Do you have a Completely course of php or wordpress?
Hi, can you help me with an urgent project similar to this. I am willing to pay. Thanks
nice))
What a great webmaster, what a great teacher, what a great Tutorial, what a Bad is my English! A Big thanks from Italy…….
Thank you so much!!!!!!
What if instead of the_title(); I wanted to grab a woocommerce product attribute and concat it to another attribute be like <?php woocommerce_product_attribute<slug1> + woocommerce_product_attribute<slug2> ?> + blanket which will out put something like "warm red blanket"