Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Get post with title %like% search term
- *
- * @param $title Post title to search for
- * @param $method wpdb method to use to retrieve results
- * @param $columns Array of column rows to retrieve
- *
- * @since 0.3
- * @return Mixed Array || False
- */
- function get_post_with_title_like( $title = null, $method = 'get_col', $columns = array ( 'ID' ) )
- {
- // sanity check ##
- if ( ! $title ) { return false; }
- // global $wpdb ##
- global $wpdb;
- // First escape the $columns, since we don't use it with $wpdb->prepare() ##
- $columns = esc_sql( $columns );
- // now implode the values, if it's an array ##
- if( is_array( $columns ) ){
- $columns = implode( ', ', $columns ); // e.g. "ID, post_title" ##
- }
- // run query ##
- $results = $wpdb->$method (
- $wpdb->prepare (
- "
- SELECT $columns
- FROM $wpdb->posts
- WHERE {$wpdb->posts}.post_title LIKE %s
- "
- , esc_sql( '%'.like_escape( trim( $title ) ).'%' )
- )
- );
- var_dump( $results );
- // return results or false ##
- return $results ? $results : false ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement