Get record from current post

I am trying to get the value of meta_value field from current post.

<?php 
    $cupost = $post->ID; 
    $registros = $wpdb->get_results( 'SELECT meta_value FROM auto_postmeta 
    WHERE post_id = $cupost AND meta_key="mg_game"');
    echo $registros[0]->meta_value . "<br/>";
?>

It does not shows the echo from $registros.

Read More

But if i put directly on the query the number of the post (post_id = 7), it shows the value of meta_value.

Related posts

1 comment

  1. Thy this out:

    <?php 
        $cupost = $post->ID;//First check if this variable gets the id, i would echo the $cupost variable to check. 
        $registros = $wpdb->get_results( "SELECT meta_value FROM auto_postmeta WHERE post_id = ".$cupost." AND meta_key= 'mg_game' ");
        echo $registros[0]->meta_value . "<br/>";
    ?>
    

    You have to concatenate the Query String with the Php Variables

Comments are closed.