Support » Developing with WordPress » Learning to “think” WP_Query

  • I suppose I’m just an old dog trying to learn new tricks. I started in 4D (anyone besides me remember that?), went to C/C++ then LAMP stacks for web apps. Been using WP for many years, but have always just “rolled my own” when it comes to db access (especially for cutsom post types with complex relationships), because that’s the way I’ve always done it. I know WP_Query has me covered… I’m just not sure how.

    By now I “think” the MySQL query when I’m trying to figure out what data I need and how to get to it. Typing complex joins and subqueries in MySQL is 2nd nature. Any resources for helping an old geezer like me transition to “thinking” WP_Query? I’ll eventually “get it” between the docs and the codebase. Just wondering if there’s a book or website that will help shortcut the transition. Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    If you’re so comfortable with MySQL, why be concerned with WP_Query? It essentially is a more accessible way (for most people) of creating DB queries without needing to learn SQL. The end result is still a SQL query. If you can create such queries on your own, WP_Query just gets gets in the way.

    Sure, there are valid reasons. To better be able to work with existing theme and plugin code. The simple desire to learn, etc. Do any of these apply to you? If not, why bother then? But if so, what would help you largely depends on how you best learn new things. We all learn differently. Some like reference books, others step by step, or video, or personal tutoring, etc.

    I personally learn by doing. Find real world projects and force myself to use the new techniques I want to learn. This way I learn what I need to know and don’t waste time learning what I don’t need. As such, official references have been nearly all that’s needed. Between that and real world examples, it’s all that’s needed. Sure, it can be a struggle at first, but I’ve rarely found other learning modes to be of much benefit.

    It sounds like you would fall back to this approach, but are hoping to ease the transition somehow. There’s not really any easier way, it’s more about finding what works for you and just doing it. I suggest you just dive in and work through it using the resources available to you such as the developer code reference. Only when you get stuck on something do you need to seek out clarification.

    Thanks for the reply, @bcworkz! I’m completely self-taught, so I guess I’m never really sure that the way I get things done is the most effecient way. Especially since I’ve seen articles all over the web warning to never, ever access the wp database through PHP. Add to that a desire to leverage as much of WP as I can to cut down on the work I have to do, and, as you say a desire to learn. I may be a geezer, but the day I stop learning is (as far as I’m concerned) the day to lie down and die. 🙂

    I dug a bit deeper in the WP guts and ran across wpdb. I must say it’s more to my liking than trying to figure out how to cram complex queries into WP_Query. Especially since I can basically just drop wpdb into my method that actually runs the query. Which allows me to drop all the connection handling code I had and leverages optimizations y’all have done that I’d never know to. 🙂

    I use the codex often, and generally find it quite helpful. In some cases, though, I find it raises as many questions as it answers (at least to me). Particularly when trying to cover a complex class in one page. More example code and breaking it into more digestable chunks would help, at least for me.

    Thanks again!

    Moderator bcworkz

    (@bcworkz)

    I’m not sure what’s wrong with accessing the DB with PHP, it’s after all what WP does. Perhaps such advice is trying have us avoid reinventing the wheel.

    In my mind I imagined you had been using the $wpdb class object to execute your SQL queries to start with. This is definitely the route for you, using mysqli PHP functions (or similar) directly can cause your code to fail to work after DB upgrades. The wpdb class adds a layer of abstraction to help ensure your code remains viable.

    In addition to the Codex, you will find more docs at https://developer.wordpress.org/ . The plugin and theme handbooks have a lot of information and examples that will prove useful even if you are just customizing your installation and are not formally writing themes or plugins. The HelpHub is still a work in progress, it is slated to replace the Codex. I’m not sure how far along it is, but what’s there could prove useful. The staging site is at https://wp-helphub.com/

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.