0

I'm using a customized version of the Last Viewed Posts plugin to grab recently viewed releases on my record label site.

The code I'm using grabs an attachment image but it is not always the page featured image - the results are intermittent. Here is the code:

function zg_recently_viewed() { // Output
echo '<ul style="list-style:none; padding-left:0px;font-size:11px;" class="viewed_posts">';
if (isset($_COOKIE["WP-LastViewedPosts"])) {
    //echo "Cookie was set.<br/>";  // For bugfixing - uncomment to see if cookie was set
    //echo $_COOKIE["WP-LastViewedPosts"]; // For bugfixing (cookie content)
    $zg_post_IDs = unserialize(preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", stripslashes($_COOKIE["WP-LastViewedPosts"]))); // Read serialized array from cooke and unserialize it
    //echo $_COOKIE["WP-LastViewedPosts"];
    foreach ($zg_post_IDs as $value) { // Do output as long there are posts
        global $wpdb;
        $zg_get_title = $wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE ID = '$value+0' AND ID IN ( SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = '5' ) LIMIT 1");
        $thumb = $wpdb->get_var("SELECT ID FROM $wpdb->posts where post_parent = '$value' and post_type = 'attachment'");
        /* $thumb = $wpdb->get_var( "SELECT meta_value FROM {$wpdb->postmeta} where post_parent = '$value' AND meta_key = '_thumbnail_id'" ); */
        $thumbnail = wp_get_attachment_image_src($thumb);
        foreach($zg_get_title as $zg_title_out) {
            if ($thumbnail) {
            echo "<li style='max-width:96px;float:left;margin-right:20px;min-height:200px;'><a class='relatedimg' style='text-decoration:none;' href=\"". get_permalink($value+0) . "\" title=\"". $zg_title_out->post_title . "\"><img src='" .$thumbnail[0]. "' alt='Release Image' />" . $zg_title_out->post_title . "</a></li>\n";
            } else {
            echo "<li style='max-width:96px;float:left;margin-right:20px;min-height:200px;'><a class='relatedimg' style='text-decoration:none;' href=\"". get_permalink($value+0) . "\" title=\"". $zg_title_out->post_title . "\"><img src='http://vizualrecords.com/wp-content/uploads/2011/04/vizual_feature-150x150.png' alt='Release Image' />" . $zg_title_out->post_title . "</a></li>\n";
            }

        }
    }
} else {
    //echo "No cookie found.";  // For bugfixing - uncomment to see if cookie     was not set
}
echo '</ul>';

}

This line: $thumb = $wpdb->get_var("SELECT ID FROM $wpdb->posts where post_parent = '$value' and post_type = 'attachment'");

...grabs an image and most often will grab the featured image for the page, but not always. The commented out line below that does not work but I think that is closer to what I'm looking for.

The cookie is set in a function above this. Note that I do need to grab the featured image with a direct mysql call ($wpdb) and not using normal WP functions.

Any ideas?

0

That is not how you would query for a featured image. This:

$thumb = $wpdb->get_var("SELECT ID FROM $wpdb->posts where post_parent = '$value' and post_type = 'attachment'");

Should be:

$thumb = $wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta where meta_key = '_thumbnail_id' and post_id = '$value'");

That is assuming that $value is a post ID.

"Featured" images are attachments, but not all attachments are featured images. That is why this works sometimes but not others.

However, you've just reinvented the wheel. What you are doing is exactly what get_post_thumbnail_id does, though that function uses proper Core mechanisms.

  • This is perfect...I looked far and wide for this little snippet so thank you. I had a workaround which was as simple as removing extraneous images attached to the page in the Media Library but this is much better. – joshuaiz Jan 21 '14 at 0:12
  • As far as reinventing the wheel, it was necessary within the confines of the Last Viewed Posts plugin in order for it to grab the featured image in relation to the posts viewed that are stored in the user's cookie. – joshuaiz Jan 21 '14 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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