Support » Plugins » $wpdb question.

  • Resolved verymossy


    I have a large lump of code that outputs a custom table for my blog. This used two database calls, one $wpdb->get var and one get_results.

    After getting a few queries I was thinking about formatting this up into a plugin. The firs thing I did was to take the perfectly functioning code, put it in a function and call it immidietly afterwards to make sure it was ok. No such luck, failed dead without even an error. After some messing around I worked out it was the db calls that were now failing spectacularly. Why is it when i put those db calls in a function they fail? How do I get around this? What am i doing wrong?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Add this before everything else in your function:

    global $wpdb;

    thanks! isn’t that done anyway?! Is having to do that normal for plugins?

    if you want to use the $wpdb variable then you need to declare it as global in every single function that uses it.

    Having to do that is normal for PHP.

    Do you have to use $wpdb outside the loop?

    Do you have to use $wpdb outside the loop?

    And the reason for your question is?

    $wpdb is the class used in WordPress to more easily interact with the database. It’s not specifically related to the post object, and one should be able to perform db queries through it in or out of The Loop.

    Kafkaesqui, I tried using $wpdb inside the loop and I just get a blank page. Outside the loop it works fine.

    Never had a problem with $wpdb in The Loop, but just saying you’re using $wpdb doesn’t tell me much. What are you doing with it?

    I was trying to use $wpdb to query the db for the ID of a post whose post slug was constructed from the title of the current post. BUT that was before I found out about permalinks which allow me to make a link to a post based on its post name rather than its ID. I can do what I need using permalinks instead of a query.

    I’m trying to make “sub posts” display a link to a “parent post” (i.e. the posts “Yellow Banana” and “Yellow Sign” both link to “Yellow”) dynamically. I have a script that converts the title to a string and clips off the last words, makes it lowercase, and converts the spaces to dashes and put it in $postslug. I was trying to have $wpdb query for a post with matching $postslug. Now that I know about permalinks I can do this, but is there an easier way?

    I really appreciate your help.

    Ah, ok.

    I’ll see about following up on this in your thread so we’re not juggling topics.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘$wpdb question.’ is closed to new replies.