WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] My query with posts and custom taxonomies wont work (2 posts)

  1. Metuza
    Member
    Posted 2 years ago #

    Hello,

    Can anyone tell me why i get this error (Unknown column 'wposts.ID' in 'on clause') on the below query? I have tried with prefix also.

    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->postmeta wpostmeta2
    INNER JOIN $wpdb->term_relationships ON(wposts.ID = $wpdb->term_relationships.object_id)
    INNER JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    JOIN $wpdb->postmeta ON(wposts.ID = $wpdb->postmeta.post_id)
    WHERE $wpdb->term_taxonomy.term_id = '$_POST[cp_auction_category]'
    AND $wpdb->term_taxonomy.taxonomy = 'auctions'
    AND wposts.ID = wpostmeta.post_id
    AND wposts.ID = wpostmeta2.post_id
    AND (wpostmeta.meta_key = 'closed' AND wpostmeta.meta_value = '0')
    AND (wpostmeta2.meta_key = 'featured' AND wpostmeta2.meta_value >= '0')
    AND wposts.post_status = 'publish'
    ORDER BY wpostmeta2.meta_value AND wposts.post_date DESC";

    Thanks
    Rune

  2. Metuza
    Member
    Posted 2 years ago #

    Solution is found, below code works great :)

    SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta m1 ON($wpdb->posts.ID = m1.post_id)
    LEFT JOIN $wpdb->postmeta m2 ON($wpdb->posts.ID = m2.post_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)
    WHERE $wpdb->term_taxonomy.term_id = '$_POST[cp_auction_category]'
    AND $wpdb->term_taxonomy.taxonomy = 'auctions'
    AND (m1.meta_key = 'closed' AND m1.meta_value = '0')
    AND (m2.meta_key = 'featured' AND m2.meta_value >= '0')
    AND $wpdb->posts.post_author='$author'
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->posts.post_type = 'auction'
    ORDER BY m2.meta_value AND $wpdb->posts.post_date DESC";

Topic Closed

This topic has been closed to new replies.

About this Topic