wp_postmeta Query Compatibility
-
I just thought I’d post this here as I’ve been having some query compatibility issues with secondary-title and my theme.
The LEFT JOIN that is made when include_in_search in switched on conflicts with standard meta queries that may be hooked elsewhere. The resulting query looks like this:
SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id ...
, and this causes a “Not unique table/alias: ‘wp_postmeta'” error.
Updating the following two functions to have a table alias sorts this out:
function secondary_title_search_join($join) { global $wpdb; if(is_search()) { $join .= " LEFT JOIN " . $wpdb->postmeta . " AS stmt1 ON " . $wpdb->posts . ".ID = stmt1.post_id "; } return $join; }
function secondary_title_search_where($where) { global $wpdb; if(is_search()) { $where = preg_replace( "/\(\s*" . $wpdb->posts . ".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", "(" . $wpdb->posts . ".post_title LIKE $1) OR (stmt1.meta_value LIKE $1)", $where ); } return $where; }
I hope this is helpful and thanks for the awesome plugin!
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘wp_postmeta Query Compatibility’ is closed to new replies.