• Resolved NathanielFisher

    (@nathanielfisher)


    I’m trying to write a simple custom database query (to go in the template) to get a meta_value from the database, but it’s not working as I’d expect.

    The following code works fine:

    <?php
    global $wpdb;
    
    $getMeta = $wpdb->get_results( 'SELECT meta_value FROM wp_postmeta WHERE post_id = 492', OBJECT);
    
    echo $getMeta[0]->meta_value;
    ?>

    It returns the meta_value, which is 4683. But I need the ID to be the ID of whatever page it’s on, so I tried simply swapping it for a variable

    <?php
    global $wpdb;
    $getId = the_ID();
    $getMeta = $wpdb->get_results( 'SELECT meta_value FROM wp_postmeta WHERE post_id = $getId', OBJECT);
    
    echo $getMeta[0]->meta_value;
    ?>

    …which, for some reason returned the post ID (which incidentally is 492) and not the meta value (4683).

    So then I tried this

    <?php
    global $wpdb;
    
    $getMeta = $wpdb->get_results( 'SELECT meta_value FROM wp_postmeta WHERE post_id = <?php the_ID(); ?>', OBJECT);
    
    echo $getMeta[0]->meta_value;
    ?>

    which returned nothing, and finally (thinking that maybe it needed to know the value of the ID before using it in a query, I tried this

    <?php
    ob_start();
    echo the_ID();
    $getId = ob_get_contents();
    ob_end_clean();
    ?>
    <?php
    global $wpdb;
    
    $getMeta = $wpdb->get_results( 'SELECT meta_value FROM wp_postmeta WHERE post_id = $getId', OBJECT);
    
    echo $getMeta[0]->meta_value;
    ?>

    which, again returned, or at least “echoed” nothing. I’m guessing the answer’s obvious to anyone who knows much about php. But after a few hours of trying to get it to work I could use some advice. I’m using php version 5.4.31 (in case it matters).

Viewing 3 replies - 1 through 3 (of 3 total)
  • linux4me2

    (@linux4me2)

    Say the meta_name of the meta_value you’re trying to get is “_my_value”. Something like this may work, but you have to use get_the_ID() within the loop, and you don’t say where in the template you’re working:

    <?php
    global $wpdb;
    $postid = get_the_ID();
    $getMeta = $wpdb->get_row("SELECT meta_value FROM wp_postmeta WHERE post_id = " . $postid . " AND meta_name = '_my_value'");
    
    echo 'meta_value = ' . $getMeta->meta_value;
    ?>

    Just substitute the meta_name of the meta_value you want returned for “_my_value”.

    Thread Starter NathanielFisher

    (@nathanielfisher)

    Thanks for your help. I think it’s within the loop. I was editing the template and adding it to single.php, above where it says end of loop. It didn’t work for me though, but there’s a very distinct possibility it was something that I did wrong.

    Here’s how I got it working in the end.

    <?php
    $result =$wpdb->get_results("SELECT meta_value FROM $wpdb->posts , $wpdb->postmeta where  $wpdb->postmeta.post_id = $post->ID
    
    AND meta_key='TYPE_META_KEY'", OBJECT);
    
    print_r($result[0]->meta_value);
    ?>
    linux4me2

    (@linux4me2)

    Cool. I’m glad you got it working.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘What's wrong with this database query?’ is closed to new replies.