Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I launched mysql and confirmed this query returns a result:

mysql> SELECT * FROM wp_posts WHERE post_parent = 68 AND post_type = 'attachment' AND post_name = 'side-logo-auto2' LIMIT 1;
1 row in set (0.00 sec)

In my template, I attempt same thing:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();

    //$wpdb->posts is wp_posts table
    //$post->ID is current post of post_type page, 
    //that is associated with page of this template

    global $wpdb;
    $image = $wpdb->get_var(
        $wpdb->prepare(
            "SELECT * FROM $wpdb->posts
             WHERE post_parent = %d
             AND post_type = %s
             AND post_name = %s
             LIMIT 1
            ",
            $post->ID,
            'attachment',
            'side-logo-auto2'
        )
    );
    echo "The image id is {$image->ID}";
    echo wp_get_attachment_image($image->ID);

endwhile; endif; ?> 

$post->ID returns 68. Yet, the value assigned to $image is not the record, but rather null.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

$wpdb->get_var returns a single variable, but your SQL statement has SELECT * which returns a row of multiple columns.

You need to change your SQL statement to SELECT ID ... instead of SELECT * ...

share|improve this answer
    
This would return just the id, but it was actually get_row what I was looking for. –  JohnMerlino Jul 5 '12 at 19:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.