Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Custom Search that I did something wrong on the select

Hello, I am doing something wrong.

I created a custom search function that searches for taxonomy_terms and postmeta. It works like a charm when I have 2/3 queries, but when I add more I end up having a 100% cpu process on mysql, and the page times out. The database currently has 2 posts.

So, here is what I did:

public static function queryTours() {
    global $wpdb;

    $query['select']="SELECT posts.ID FROM {$wpdb->posts} AS posts ";
    $query['select'].="INNER JOIN {$wpdb->term_relationships} AS destinations ON posts.ID=destinations.object_id ";
    $query['select'].="INNER JOIN {$wpdb->term_relationships} AS types ON posts.ID=types.object_id ";
    $query['select'].="INNER JOIN {$wpdb->term_relationships} AS category ON posts.ID=category.object_id ";
    $query['select'].="INNER JOIN {$wpdb->term_relationships} AS classification ON posts.ID=classification.object_id ";
    $query['select'].="INNER JOIN {$wpdb->term_relationships} AS capacity ON posts.ID=capacity.object_id ";
    $query['select'].="INNER JOIN {$wpdb->term_relationships} AS rooms ON posts.ID=rooms.object_id ";
    $query['select'].="INNER JOIN {$wpdb->term_relationships} AS features ON posts.ID=features.object_id ";
    $query['select'].="INNER JOIN {$wpdb->postmeta} AS prices ON (posts.ID=prices.post_id AND prices.meta_key = '_".THEME_PREFIX."price') ";
    $query['select'].="INNER JOIN {$wpdb->postmeta} AS durations ON (posts.ID=durations.post_id AND durations.meta_key = '_".THEME_PREFIX."duration') ";
    $query['select'].="INNER JOIN {$wpdb->postmeta} AS dates_departure ON (posts.ID=dates_departure.post_id AND dates_departure.meta_key = '_".THEME_PREFIX."date_departure') ";
    $query['select'].="INNER JOIN {$wpdb->postmeta} AS dates_arrival ON (posts.ID=dates_arrival.post_id AND dates_arrival.meta_key = '_".THEME_PREFIX."date_arrival') ";

    $query['where']="WHERE (posts.post_status = 'publish' OR posts.post_status = 'private') ";
    $query['where'].="AND posts.post_type = 'tour' ";       

Does anyone have any tips I could do to fix it? I'm kinda lost on this for about a week :/

I think I shouldn't be using INNER JOINS on the same table, but I don't know how else to do. Thank you guys!