WordPress.org

Forums

[resolved] Can't get WP tag from a sql query for plugin (10 posts)

  1. Arlo
    Member
    Posted 8 years ago #

    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.

  2. muskokee
    Member
    Posted 8 years ago #

    Try this:

    $query = sprintf("SELECT * FROM %s WHERE title='". $post->post_title ."' LIMIT 1");
  3. Arlo
    Member
    Posted 8 years ago #

    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?

  4. muskokee
    Member
    Posted 8 years ago #

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

    $posted = $post->post_title;

  5. Arlo
    Member
    Posted 8 years ago #

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

  6. muskokee
    Member
    Posted 8 years ago #

    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;

  7. Arlo
    Member
    Posted 8 years ago #

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

  8. muskokee
    Member
    Posted 8 years ago #

    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 :-)

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

  10. Arlo
    Member
    Posted 8 years ago #

    Hey Thanks Otto, that is simpler. It's not rain, it's enlightenment :)

Topic Closed

This topic has been closed to new replies.

About this Topic