WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Can’t get WP tag from a sql query for plugin

[Resolved] Can’t get WP tag from a sql query for plugin

  • I have a this db query in a plugin:

    $query = sprintf("SELECT * FROM %s WHERE title='$post->post_title' LIMIT 1;"

    It’s returning null on $post->post_title. It works fine if I put a fixed value there, and also works fine if I hard-code it into the template loop.

    So I know the tag is being ignored in the plugin, I just can’t figure out why, or what a proper syntax would be. Any help is appreciated.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Try this:

    $query = sprintf("SELECT * FROM %s WHERE title='". $post->post_title ."' LIMIT 1");

    Thanks for the reply muskokee. That’s actually a variant I tried earlier, no go.

    Perhaps because the function is in a plugin and not directly in the loop it needs to be referenced differently somehow?

    how about if you load the post title into a variable first, then use the variable in the query?

    $posted = $post->post_title;

    Nope. This is making me nuts. The plugin simply doesn’t recognize the loop tag no matter how I reference it.

    Is the query in a function? If it is have you called $posts through global?

    What plugin is it?

    EDIT: Silly me, I should have reread the above posts. Yup, it is a function. Check to make sure that right after the function name is

    global $posts;

    Ah that was it, duh! I didn’t have $post in global. Thanks!

    That’s fabulous! I was just sending a message through to fix the global $posts; to global $post; . I’m glad it’s sorted out 🙂

    Not to rain on your parade, but a slightly more acceptable way to get the title is to call the function made specifically to do that. Namely, get_the_title(). No need to do global anything then.

    Hey Thanks Otto, that is simpler. It’s not rain, it’s enlightenment 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] Can’t get WP tag from a sql query for plugin’ is closed to new replies.
Skip to toolbar