0

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.

2 Answers 2

1

I'd try to get rid of custom query altogether. Try something like:

$attachments = get_posts(array(
  'post_type' => 'attachment',
  'post_parent' => get_the_ID(),
  'numberposts' => 1,
));
4
  • thanks.. I can not get anything to work with that. Can you please elaborate
    – zac
    Jul 16, 2011 at 7:54
  • @zac does query like in my answer (maybe needs tweaking) return anything for you? It should get you first attachment that has current post as parent.
    – Rarst
    Jul 16, 2011 at 7:57
  • It returns the images, but not to a specific tag, and not from the custom posts. My original question I was able to pull attachments for just those tags, but it did not work with my custom post type
    – zac
    Jul 17, 2011 at 8:13
  • @zac sorry, I am not following. Your custom query searches for attachments that have current post in the loop as parent (get_the_ID()). It has nothing to do with tags.
    – Rarst
    Jul 17, 2011 at 8:53
0

Of course after I offer up a bounty I immediately find the answer. It just took me a while to figure out what the question was... It seems that custom post types are not designed to work in archives like this. I did find a function the allows this to work:

Custom Post Type Archives by Date and Taxonomy

1
  • 1
    That question/answers are very outdated. Custom Post Types got archive support since WP 3.1, see has_archive when registering and such.
    – Rarst
    Jul 17, 2011 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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