Changing my earlier post as I realised it wasn't the way I thought, and included most of below is the search file used on my website, it searches in posts table title and IDs, I want it to search users table too, so if the keyword matches a user_nicename then it should include the user name or at least users posts in the search result.
enter code here````if( !class_exists( 'Adifier_Advert_Query' ) ){
public $args;
function __construct( $args = array() ) {
$args['orderby'] = empty( $args['orderby'] ) ? 'expire' : $args['orderby'];
$args['order'] = empty( $args['order'] ) ? 'DESC' : $args['order'];
$args = array_merge( array(
'post_type' => 'advert',
'orderby' => 'expire',
'order' => 'DESC',
'post_parent' => 0,
'status' => '',
'posts_per_page' => adifier_get_option( 'adverts_per_page' ),
'return_all' => false,
'expired' => false,
'sold' => false,
'urgent' => false,
'include_top_ads' => false,
), $args);
$this->args = $args;
add_filter( 'posts_fields', array( $this, 'posts_fields' ) );
add_filter( 'posts_join', array( $this, 'posts_join' ) );
add_filter( 'posts_where', array( $this, 'posts_where' ) );
add_filter( 'posts_orderby', array( $this, 'posts_orderby' ));
if( $this->args['include_top_ads'] ){
add_filter( 'posts_request', array( $this, 'posts_request' ));
}
parent::__construct( $args );
adifier_clear_filter( 'posts_fields', array( $this, 'posts_fields' ) );
adifier_clear_filter( 'posts_join', array( $this, 'posts_join' ) );
adifier_clear_filter( 'posts_where', array( $this, 'posts_where' ) );
adifier_clear_filter( 'posts_orderby', array( $this, 'posts_orderby' ));
if( $this->args['include_top_ads'] ){
adifier_clear_filter( 'posts_request', array( $this, 'posts_request' ));
}
}
/*
* In order to provide top ads which will list with regular ones we need to use UNION ALL
* This will be applied on completed query for searching
*/
static public function posts_request_process( $sql, $ids, $posts_per_page, $paged ){
if( !empty( $ids ) ){
global $wpdb;
$ids = array_unique( $ids );
$sql = preg_replace( "/LIMIT.*/", '', $sql );
preg_match("/ORDER BY.*/", $sql, $match);
$orderby = !empty( $match[0] ) ? str_replace( array('adverts.', 'ORDER BY ', $wpdb->posts.'.'), '', $match[0] ) : '';
$sql = $sql1 = $sql2 = preg_replace( "/ORDER.*/", '', $sql );
if( strstr( $orderby, 'post_date' ) ){
$select_fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_author, FROM_UNIXTIME(1544822069+FLOOR(RAND()*31536000)) AS post_date, {$wpdb->posts}.post_date_gmt, {$wpdb->posts}.post_content, {$wpdb->posts}.post_title, {$wpdb->posts}.post_excerpt, {$wpdb->posts}.post_status, {$wpdb->posts}.comment_status, {$wpdb->posts}.ping_status, {$wpdb->posts}.post_password, {$wpdb->posts}.post_name, {$wpdb->posts}.to_ping, {$wpdb->posts}.pinged, {$wpdb->posts}.post_modified, {$wpdb->posts}.post_modified_gmt, {$wpdb->posts}.post_content_filtered, {$wpdb->posts}.post_parent, {$wpdb->posts}.guid, {$wpdb->posts}.menu_order, {$wpdb->posts}.post_type, {$wpdb->posts}.post_mime_type, {$wpdb->posts}.comment_count ";
$sql1 = str_replace( $wpdb->posts.'.*', $select_fields, $sql1 );
}
$sql1 = str_replace( array( 'SQL_CALC_FOUND_ROWS', 'AND adverts.expire'), array( 'SQL_CALC_FOUND_ROWS 1 AS topad, ', 'AND '.$wpdb->posts.'.ID IN ('.join( ',', $ids ).') AND adverts.expire' ), $sql1 );
$sql2 = str_replace( 'SQL_CALC_FOUND_ROWS', '0 AS topad, ', $sql2 );
$offset = ( $paged - 1 ) * $posts_per_page;
return "({$sql1}) UNION ALL ({$sql2}) ORDER BY topad DESC, {$orderby} LIMIT ".$wpdb->prepare( "%d, %d", $offset, $posts_per_page);
}
else{
return $sql;
}
}
function posts_request( $sql ){
$top_ads = adifier_get_top_ads_list();
$ids = array();
if( !empty( $top_ads ) ){
if( !empty( $_POST['category'] ) && !empty( $top_ads[$_POST['category']] ) ){
$ids = array_keys( $top_ads[$_POST['category']] );
}
else{
foreach( $top_ads as $term_id => $data ){
$ids = array_merge( $ids, array_keys( $data ) );
}
}
}
if( !empty( $ids ) ){
return self::posts_request_process( $sql, $ids, $this->args['posts_per_page'], $this->args['paged'] );
}
else{
return $sql;
}
}
/*
* Select all values from adifier_advert_meta_data table
*/
static public function posts_main_fields( $sql ){
return $sql . ", adverts.*, ".self::currency_filter()." AS sort_price ";
}
function posts_fields( $sql ) {
return self::posts_main_fields( $sql );
}
/*
* Join adifier_advert_data table and all relationships for range searches
*/
static public function posts_main_join( $sql ){
global $wpdb;
$sql .= " INNER JOIN {$wpdb->prefix}adifier_advert_data AS adverts ON $wpdb->posts.ID = adverts.post_id ";
return $sql;
}
function posts_join( $sql ) {
global $wpdb;
$sql = self::posts_main_join( $sql );
if( !empty( $this->args['tax_query_between'] ) ){
foreach( $this->args['tax_query_between'] as $key => $data ){
$unique = esc_sql( preg_replace("/[^a-z]+/", "", $data['taxonomy']) ).'_'.esc_sql( $key );
$sql .= " LEFT JOIN {$wpdb->prefix}term_relationships AS btr{$unique} ON $wpdb->posts.ID = btr{$unique}.object_id ";
}
}
return $sql;
}
static public function posts_where_not_expired( $sql ){
global $wpdb;
$sql .= $wpdb->prepare( " AND adverts.expire > %d ", current_time('timestamp') );
/* this is applied onbly on public queries so we will add filter by inactive users here */
$inactive = get_option( 'adifier_inactive_users' );
if( !empty( $inactive ) ){
$sql .= " AND {$wpdb->posts}.post_author NOT IN (".join( ',', $inactive ).") ";
}
return $sql;
}
function posts_where( $sql ) {
global $wpdb;
if( !empty( $this->args['s'] ) ){ $sh = $args['s'];
$nowhere = str_replace('(' .$wpdb->users . '.ID LIKE', '(' . $wpdb->users . '.user_nicename LIKE ' . $sh . ') OR (' . $wpdb->users . '.ID LIKE', $nowhere); echo "$nowhere";}
/* if keyword is set and it is numeric search for ID as well */
if( !empty( $this->args['s'] ) ){
if( is_numeric( $this->args['s'] ) ){
$sql = str_replace( "({$wpdb->posts}.post_title LIKE", $wpdb->prepare( "({$wpdb->posts}.ID LIKE %s) OR ({$wpdb->posts}.post_title LIKE", $this->args['s'] ), $sql);
}
}
source
https://stackoverflow.com/questions/61953484/search-users-and-posts-wp