Skip to content

Instantly share code, notes, and snippets.

@salcode
Created March 31, 2016 15:50
Show Gist options
  • Save salcode/2aefd99f58635ae25ddc2b6a3a7b65ed to your computer and use it in GitHub Desktop.
Save salcode/2aefd99f58635ae25ddc2b6a3a7b65ed to your computer and use it in GitHub Desktop.
WordPress mu-plugin to add support for "title_like" in WP_Query to add a LIKE WHERE clause to a query.
<?php
/**
* Example usage to find all Titles with a comma:
* $example_query = new WP_Query(
* array(
* 'title_like' => ','
* )
* );
*/
add_filter( 'posts_where', 'fe_title_like_where', 10, 2 );
function fe_title_like_where( $where, &$wp_query ) {
global $wpdb;
if ( $title_like = $wp_query->get( 'title_like' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . $wpdb->esc_like( $title_like ) . '%\'';
}
return $where;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment