How change post_from with query filter?

Hi i m tring to ovveride a plugin query to database. I m succeded to change post_where in this way.

remove_filter( ‘posts_where’, ‘gmw_ps_pt_query_keywords’, 20 );
add_filter( ‘posts_where’, ‘gmw_ps_pt_query_keywords_tags’, 20, 2 );

function gmw_ps_pt_query_keywords_tags( $where, $wp_query ) {

$gmw_args = $wp_query->get( ‘gmw_args’ );

// abort if this is not GMW Form.
if ( empty( $gmw_args ) ) {
return $where;
}

// this value is set on form submission.
if ( empty( $gmw_args[‘keywords’] ) ) {
return $where;
}

global $wpdb;

// get keywords value from URL.
$keywords = $gmw_args[‘keywords’];

// support for WordPress lower then V4.0.
$like = method_exists( $wpdb, ‘esc_like’ ) ? $wpdb->esc_like( trim( $keywords ) ) : like_escape( trim( $keywords ) );
$like = esc_sql( $like );
$like = ‘%’ . $like . ‘%’;

// search title.
$query = “{$wpdb->posts}.post_title LIKE ‘{$like}'”;

// search content.
if ( ! apply_filters( ‘gmw_ps_disable_keywords_content_search’, false ) ) {
$query .= ” OR {$wpdb->posts}.post_content LIKE ‘{$like}’ OR {$wpdb->posts}.post_excerpt LIKE ‘{$like}'”;
}

// search tag

$query .=” OR (“;
$query .=” {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id “;
$query .=” AND {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id “;
$query .=” AND {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id “;
$query .=” AND {$wpdb->terms}.name LIKE ‘{$like}'”;
$query .= “)”;

$where .= ” AND ({$query})”;

return $where;

}

But I have this error
Errore sul database di WordPress: [Unknown column ‘wp_term_relationships.object_id’ in ‘where clause’]

SELECT SQL_CALC_FOUND_ROWS wp_posts.*, gmw_locations.ID as location_id, gmw_locations.object_type, gmw_locations.object_id, gmw_locations.user_id, gmw_locations.latitude as lat,gmw_locations.latitude, gmw_locations.longitude as lng,gmw_locations.longitude, gmw_locations.street_name, gmw_locations.street_number, gmw_locations.street, gmw_locations.premise, gmw_locations.city, gmw_locations.region_name, gmw_locations.region_code, gmw_locations.postcode, gmw_locations.country_name, gmw_locations.country_code, gmw_locations.address, gmw_locations.formatted_address FROM wp_posts LEFT JOIN wp_gmw_locations gmw_locations ON wp_posts.ID = gmw_locations.object_id AND gmw_locations.object_type = ‘post’ WHERE 1=1 AND wp_posts.post_type IN (‘product’, ‘convenzione’) AND ((wp_posts.post_status = ‘publish’)) AND (wp_posts.post_title LIKE ‘%centro%’ OR wp_posts.post_content LIKE ‘%centro%’ OR wp_posts.post_excerpt LIKE ‘%centro%’ OR ( wp_posts.ID = wp_term_relationships.object_id AND wp_terms.term_id = wp_term_taxonomy.term_id AND wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id AND wp_terms.name LIKE ‘%centro%’)) AND (wp_posts.post_title LIKE ‘%centro%’ OR wp_posts.post_content LIKE ‘%centro%’ OR wp_posts.post_excerpt LIKE ‘%centro%’) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 5

If i change manually the query from
FROM wp_posts LEFT JOIN
to
FROM wp_term_relationships,wp_terms,wp_term_taxonomy, wp_posts
LEFT JOIN
It works.
How can change with FROM field of query ?

Thanks in advance

Read more here:: How change post_from with query filter?

Leave a Reply

Your email address will not be published. Required fields are marked *