WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] $wpdb question. (11 posts)

  1. verymossy
    Member
    Posted 8 years ago #

    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?

  2. Kafkaesqui

    Posted 8 years ago #

    Add this before everything else in your function:

    global $wpdb;

  3. verymossy
    Member
    Posted 8 years ago #

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

  4. Ajay
    Member
    Posted 8 years ago #

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

  5. Kafkaesqui

    Posted 8 years ago #

    Having to do that is normal for PHP.

  6. direx
    Member
    Posted 8 years ago #

    Do you have to use $wpdb outside the loop?

  7. Kafkaesqui

    Posted 8 years ago #

    "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.

  8. direx
    Member
    Posted 8 years ago #

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

  9. Kafkaesqui

    Posted 8 years ago #

    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?

  10. direx
    Member
    Posted 8 years ago #

    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.

  11. Kafkaesqui

    Posted 8 years ago #

    Ah, ok.

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

Topic Closed

This topic has been closed to new replies.

About this Topic