I have a tag page that is generated with the first image attachments from all of the posts. I call it in the loop with :
<img src="<?php echo get_first_attachment() ?>" />
and this is the function :
function get_first_attachment(){
$querystr =
"
SELECT
wp_posts.post_excerpt AS 'imageTitle',
wp_posts.guid AS 'imageGuid'
FROM
wp_posts
WHERE
wp_posts.post_parent = ".get_the_ID()."
AND wp_posts.post_type = \"attachment\"
ORDER BY \"menu_order\"
LIMIT 1
";
global $wpdb;
$url = get_bloginfo('url');
$post_item = $wpdb->get_row($querystr);
$first_attachment = $post_item->imageGuid;
$not_broken = @fopen("$first_attachment","r"); // checks if the image exists
if(empty($first_attachment) || !($not_broken)){ //Defines a default image
unset($first_attachment);
}else{
$first_attachment = str_replace($url, '', $first_attachment);
}
return $first_attachment;
}
This works great with regular posts but not my custom post types. Which are in the post_type portfolio.
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 4,
'taxonomies' => array('post_tag','category'),
'supports' => array('title','editor','comments','trackbacks','revisions','custom-fields','page-attributes','thumbnail', 'excerpt', 'tags')
);
register_post_type( 'portfolio' , $args );
}
Looking at the tables Both post and portfolio share post_type = attachment, however different in that one has post_type = post and the other = portfolio.
I do not understand the SELECT .. post_excerpt is a blank column and guid is just full of urls. I am confused. Thanks for any help.
I figured out that the image attachment has nothing to do with it, my custom post type is not being included in the tag loop... I will post more code once I figure out what it is I am looking for.