• Resolved ricardormz

    (@ricardormz)


    Hello. Most of the time I found answers searching the forum, but not this time. Can anyone help me on this query.

    What am i doing wrong?

    <?php
    global $wpdb;
    $attachment_id = $wpdb->get_var("
    	SELECT
    	$wpdb->posts.ID
    	LEFT JOIN
    	$wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
    	LEFT JOIN
    	$wpdb->term_taxonomy ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
    	FROM $wpdb->posts
    	WHERE $wpdb->posts.post_type='attachment'
    	AND $wpdb->posts.post_title='header'
    	AND $wpdb->term_taxonomy.taxonomy = 'category'
    	AND $wpdb->term_taxonomy.term_id = '5'
    	ORDER BY post_date DESC LIMIT 1");
    ?><?php echo get_attachment_icon($attachment_id); ?>

    What i’m trying to do is to put on the sidebar the latest post thumb (with “header” as image title) from an specific category. This is the first time i try to write my own query.

    Thanks

Viewing 1 replies (of 1 total)
  • Thread Starter ricardormz

    (@ricardormz)

    It seems that by default attachments are uncategorized, thats why the result was null. I had to install this plugin

    Attachment Category Updater Plugin

    “Normally (at least with WordPress 2.3.2), the categories of post attachments do not get updated when the post categories change. There is also no way to manually make these updates through the admin. The Attachment Category Updater keeps your posts and attachments in sync.”

    The query was OK. The attachment’s category wasn’t.

Viewing 1 replies (of 1 total)
  • The topic ‘Help on mysql query (select attachment ID)’ is closed to new replies.