Skip to main content
Quality and why use prepare and stick a var inside the prepare when it should be a param if its not a string.
Source Link

I figured out a solution to my question.

First of all, in my original query, I should have specified OR instead of AND for searching between group names and group descriptions. (It was skewing the results.)

And I needed to double escape my '%'s in the LIKE statements.

Here is the updated query which works correctly:

SELECT * 
FROM {$wpdb->prefix}bp_groups 
WHERE `name` LIKE '%%%s%%'
OR `description` LIKE '%%%s%%' 
AND `status` = 'public'
ORDER BY `name` ASC

Example using $wpdb->query and $wpdb->prepare:

global $wpdb;
$wpdb->query( $wpdb->prepare(
    "
    DELETE 
   'DELETE FROM {$wpdb->options}
   %s WHERE `option_name`
    LIKE %s%s',
    "$wpdb->options,
    $wpdb->esc_like(PLUGIN_SLUG . '%%')
) );

I figured out a solution to my question.

First of all, in my original query, I should have specified OR instead of AND for searching between group names and group descriptions. (It was skewing the results.)

And I needed to double escape my '%'s in the LIKE statements.

Here is the updated query which works correctly:

SELECT * 
FROM {$wpdb->prefix}bp_groups 
WHERE `name` LIKE '%%%s%%'
OR `description` LIKE '%%%s%%' 
AND `status` = 'public'
ORDER BY `name` ASC

Example using $wpdb->query and $wpdb->prepare:

global $wpdb;
$wpdb->query( $wpdb->prepare(
    "
    DELETE 
    FROM {$wpdb->options}
    WHERE `option_name`
    LIKE %s
    ",
    PLUGIN_SLUG . '%%'
) );

I figured out a solution to my question.

First of all, in my original query, I should have specified OR instead of AND for searching between group names and group descriptions. (It was skewing the results.)

And I needed to double escape my '%'s in the LIKE statements.

Here is the updated query which works correctly:

SELECT * 
FROM {$wpdb->prefix}bp_groups 
WHERE `name` LIKE '%%%s%%'
OR `description` LIKE '%%%s%%' 
AND `status` = 'public'
ORDER BY `name` ASC

Example using $wpdb->query and $wpdb->prepare:

global $wpdb;
$wpdb->query( $wpdb->prepare(
    'DELETE FROM %s WHERE `option_name` LIKE %s',
    $wpdb->options,
    $wpdb->esc_like(PLUGIN_SLUG . '%%')
) );
added 248 characters in body
Source Link
Michael Ecklund
  • 6.8k
  • 13
  • 68
  • 110

I figured out a solution to my question.

First of all, in my original query, I should have specified OR instead of AND for searching between group names and group descriptions. (It was skewing the results.)

And I needed to double escape my '%'s in the LIKE statements.

Here is the updated query which works correctly:

SELECT * 
FROM {$wpdb->prefix}bp_groups 
WHERE `name` LIKE '%%%s%%'
OR `description` LIKE '%%%s%%' 
AND `status` = 'public'
ORDER BY `name` ASC

Example using $wpdb->query and $wpdb->prepare:

global $wpdb;
$wpdb->query( $wpdb->prepare(
    "
    DELETE 
    FROM {$wpdb->options}
    WHERE `option_name`
    LIKE %s
    ",
    PLUGIN_SLUG . '%%'
) );

I figured out a solution to my question.

First of all, in my original query, I should have specified OR instead of AND for searching between group names and group descriptions. (It was skewing the results.)

And I needed to double escape my '%'s in the LIKE statements.

Here is the updated query which works correctly:

SELECT * 
FROM {$wpdb->prefix}bp_groups 
WHERE `name` LIKE '%%%s%%'
OR `description` LIKE '%%%s%%' 
AND `status` = 'public'
ORDER BY `name` ASC

I figured out a solution to my question.

First of all, in my original query, I should have specified OR instead of AND for searching between group names and group descriptions. (It was skewing the results.)

And I needed to double escape my '%'s in the LIKE statements.

Here is the updated query which works correctly:

SELECT * 
FROM {$wpdb->prefix}bp_groups 
WHERE `name` LIKE '%%%s%%'
OR `description` LIKE '%%%s%%' 
AND `status` = 'public'
ORDER BY `name` ASC

Example using $wpdb->query and $wpdb->prepare:

global $wpdb;
$wpdb->query( $wpdb->prepare(
    "
    DELETE 
    FROM {$wpdb->options}
    WHERE `option_name`
    LIKE %s
    ",
    PLUGIN_SLUG . '%%'
) );
added 1 characters in body
Source Link
Michael Ecklund
  • 6.8k
  • 13
  • 68
  • 110

I figured out a solution to my problemquestion.

First of all, in my original query, I should have specified OR instead of AND for searching between group names and group descriptions. (It was skewing the results.)

And I needed to double escape my '%'s in the LIKE statements.

Here is the updated query which works correctly:

SELECT * 
FROM {$wpdb->prefix}bp_groups 
WHERE `name` LIKE '%%%s%%'
OR `description` LIKE '%%%s%%' 
AND `status` = 'public'
ORDER BY `name` ASC

I figured out a solution to my problem.

First of all, in my original query, I should have specified OR instead of AND for searching between group names and group descriptions. (It was skewing the results.)

And I needed to double escape my '%'s in the LIKE statements.

Here is the updated query which works correctly:

SELECT * 
FROM {$wpdb->prefix}bp_groups 
WHERE `name` LIKE '%%%s%%'
OR `description` LIKE '%%%s%%' 
AND `status` = 'public'
ORDER BY `name` ASC

I figured out a solution to my question.

First of all, in my original query, I should have specified OR instead of AND for searching between group names and group descriptions. (It was skewing the results.)

And I needed to double escape my '%'s in the LIKE statements.

Here is the updated query which works correctly:

SELECT * 
FROM {$wpdb->prefix}bp_groups 
WHERE `name` LIKE '%%%s%%'
OR `description` LIKE '%%%s%%' 
AND `status` = 'public'
ORDER BY `name` ASC
Source Link
Michael Ecklund
  • 6.8k
  • 13
  • 68
  • 110
Loading