Created
March 31, 2016 15:50
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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