Jump to content

wp plugin search question


andr923

Recommended Posts

Hey everyone, I would really appreciate your help on this.  I'm editing this search function for a support tickets plugin on WordPress and I can't get the search function to operate like I want it to.  Basically the plugin is for residents to supply a form requesting maintenance.  The support plugin sends some default keys to its database and a few keys that I could add myself it sends as "meta" keys and "meta values".  As is I have edited the search so that I can search for support tickets based on just one "meta" key, but as soon as I type in a search string that might be in two different keys the search returns null.  Basically, I have two meta keys "building" and "unit_number" that I would be able to search for at the same time, but it will only search through one or the other.  Weirdly enough, I can search through "building" and "author_name" (author name is one of the defaults) at the same time, or "unit_number" and "author_name" but not two meta keys. 

 

Any help would be soooo much appreciated.  I'm sure it's a small thing I can't figure out, but it would go a long way!  here's the code...

 

function suptic_icl_get_tickets_by_language( $lang = '', $args = '' ) {
global $wpdb;

if ( ! $lang )
	return suptic_get_tickets( $args );

$defaults = array(
	'perpage' => 0,
	'offset' => 0,
	'orderby' => 'id',
	'status' => array(),
	'search' => '' );

$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );

$tickets_table = suptic_db_table( 'tickets' );
$messages_table = suptic_db_table( 'messages' );
$meta_table = suptic_db_table( 'meta' );
$tr_table = $wpdb->prefix . 'icl_translations';
$query = "SELECT DISTINCT tickets.* FROM $tickets_table as tickets"
	. " INNER JOIN $tr_table AS tc ON tickets.id = tc.element_id"
	. " AND tc.element_type LIKE 'suptic-ticket'"
	. " LEFT JOIN $messages_table AS messages ON ticket_id = tickets.id"
	. " LEFT JOIN $meta_table AS meta ON object_type LIKE 'ticket' AND object_id = tickets.id"
	. " WHERE ticket_type LIKE 'ticket'"
	. $wpdb->prepare( " AND language_code LIKE %s", $lang );

if ( is_array( $status ) && ! empty( $status ) ) {
	$query .= " AND (1=0";
	foreach ( $status as $s ) {
		$query .= $wpdb->prepare( " OR tickets.status LIKE %s", '%' . $s . '%' );
	}
	$query .= ")";
}

if ( $search = trim( stripslashes( $search ) ) ) {
	$search = explode( ' ', $search );
	foreach( $search as $s ) {
		$query .= " AND (1 = 0";
		$query .= $wpdb->prepare( " OR subject LIKE %s", '%' . $s . '%' );
		$query .= $wpdb->prepare( " OR author_email LIKE %s", '%' . $s . '%' );
		$query .= $wpdb->prepare( " OR author_first_name LIKE %s", '%' . $s . '%' );
		$query .= $wpdb->prepare( " OR author_last_name LIKE %s", '%' . $s . '%' );
		$query .= $wpdb->prepare( " OR message_body LIKE %s", '%' . $s . '%' );
		$query .= $wpdb->prepare( " OR (meta_key NOT LIKE '_%' AND meta_value LIKE %s)",
			'%' . $s . '%' );
		$query .= ")";
	}
}

if ( 'update_time' == $orderby )
	$query .= " ORDER BY tickets.update_time DESC";
elseif ( 'create_time' == $orderby )
	$query .= " ORDER BY tickets.create_time DESC";
else
	$query .= " ORDER BY tickets.id ASC";

if ( $offset && $perpage ) {
	$query .= $wpdb->prepare( " LIMIT %d,%d", $offset, $perpage );
} elseif ( $perpage ) {
	$query .= $wpdb->prepare( " LIMIT %d", $perpage );
}

if ( $result = $wpdb->get_results( $query ) ) {
	$tickets = array();
	foreach ( $result as $t ) {
		$tickets[] = new SupTic_Ticket( $t );
	}
	return $tickets;
}

return null;
}

 

Thanks a lot!!!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.