• Resolved Jairo Ochoa

    (@jairoochoa)


    Hi,

    I had to make a query with wpdb and this is the query:

    $sql = "
      SELECT * 
      FROM {$wpdb->prefix}posts as p
      INNER JOIN {$wpdb->prefix}postmeta as pm
      ON p.ID = pm.post_id
      WHERE 1=1 
        AND pm.meta_key = 'subtitle'
        AND ( p.post_title LIKE '%".$s."%' OR p.post_content LIKE '%".$s."%' OR p.post_excerpt LIKE '%".$s."%' OR pm.meta_value LIKE '%".$s."%' )
        AND p.post_status = 'publish'
        AND p.post_type IN ('insurance')
        AND p.post_type NOT IN ('revision')
      ORDER BY p.post_title LIKE '%".$s."%' ASC
    ";

    The only way to exclude ID is adding by hand like this:

    AND p.ID NOT IN (1500,1498,1496,1492,1490,1485,1482,98,100,1494,2,744,1063,1280,1618,254,1581)

    Is there any way to get this array from the plugin ? so then I would concatenate to my query.

    Many thanks in advance.

    Regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Jairo Ochoa

    (@jairoochoa)

    Hi,

    Finally I’ve solved it:

    $plugins = get_option('active_plugins');
    $required_plugin = 'search-exclude/search-exclude.php';
    $sql_exclude = '';
    
    if ( in_array( $required_plugin , $plugins ) ) {
    	$exclude = get_option('sep_exclude');
    	if ( !empty( $exclude ) ) {
    		$sql_exclude = "    AND p.ID NOT IN (".implode(",", $exclude).")";
    	}
    }

    Then this string is concatenated to the $sql string.

    Maybe do you suggest a better solution ?

    Regards

    Plugin Contributor pronskiy

    (@pronskiy)

    Hi @jairoochoa,

    Your solution looks good to me 👍Thanks for sharing!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘When creating a query with wpdb exclude IDs are missing’ is closed to new replies.