WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom SQL Query inside loop (7 posts)

  1. direx
    Member
    Posted 8 years ago #

    In my header, I'm trying to make wordpress automatically generate a hyperlink to a post using a custom SQL query. The query is supposed to fetch the ID of the post to be linked based on a variable generated from the title of the current post. The query works when I execute it in MySQL, but in wordpress it doesn't, I just get a blank page.

    Can I execute sql query's from within the loop? If not, how do I use information from the current post as variables for my query?

    Let me know if you need more detail. Thanks for any help.

  2. lunabyte
    Member
    Posted 8 years ago #

    Sure, why not. That's essentially what the template tags are doing.

  3. Chris_K
    Member
    Posted 8 years ago #

    There are tags that do automatically what you're trying to do manually. I'm not sure why you don't use 'em?

  4. direx
    Member
    Posted 8 years ago #

    Ok, what I'm doing is using wordpress as a cms. the user makes a post titled "Restaurant Name Menu" and another post titled "Restaurant Name Review" and my php clips off the last word, makes it lowercase, and turns all the spaces into dashes and sticks the output into a variable. My sql searches the db for the ID of Restaurant Name using the slug that I made and puts the results into a hyperlink to the "parent restaurant."

    Other things I'm hoping to do is populate menus with links to posts matching certain meta values and also display meta information of the "Parent Restaurant" post rather than the current post.

    I've noticed most of the tags have to do with retreiving whole posts or info from the current post, but I need to get info from all over the place.

    Is there a tag that does this? Am I making this complicated?

  5. Kafkaesqui

    Posted 8 years ago #

    [Coming over from here]

    direx, you note in the other thread that using $wpdb to query the database is causing problems in The Loop. I suggest trying this formulation of a WordPress db query and see if it works for you:

    <?php
    global $wpdb;
    $id = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_name = '$YOUR_SLUG_VAR'");

    $url = get_permalink($id);
    ?>

    Should work in The Loop. $YOUR_SLUG_VAR here represents the variable holding the 'restaurant-name' post slug you're generating. Just replace with your var. $url will hold the permalink to the post, assuming everything works as expected.

    If this fails to work, post the code you've written to do this here:

    http://paste.uni.cc

    and reply back with a link to it.

  6. direx
    Member
    Posted 8 years ago #

    Works perfectly. Thanks, Kafkaesqui. You're a pro.

    That query is the exact one I was using except for the global part. Its pretty much the same as the http://wordpress.org/support/topic/64050?replies=11 "$wpdb question" post except I couldn't get it to work until now. What does global do btw?

    That code also allowed me to use get_post_meta() to fetch unique meta data from $id, the "parent post." This makes it so easy to get any type of info from any post.

    I love wordpress. Its so flexible and scalable. Obviously, not everything is as complicated as it first seems. Thanks for the help.

  7. Kafkaesqui

    Posted 8 years ago #

    "What does global do btw?"

    http://php.net/global

Topic Closed

This topic has been closed to new replies.

About this Topic