Replicating the WP_Query ‘s’ param with $wpdb

  1. It seems that you in your INNER JOIN, you are joining on

    wp_term_relationships.object_id == wp_posts.post_title
    

    which is erroneous, since object_id is the post-id in this case-scenario.

    I want all IDs (post-ids)

    SELECT ID FROM $wpdb->posts
    

    Who have a record in the wp_term_relationships table

    // Now we are comparing the correct columns
    INNER JOIN $wpdb->term_relationships ON $wpdb->term_relationships.object_id = $wpdb->posts.ID 
    
  2. Adding the additional where clause

    Where the term_taxonomy_id is equal to 2

    WHERE $wpdb->term_relationships.term_taxonomy_id = 2