Search
 
SCRIPT & CODE EXAMPLE
 

PHP

limit wordpress search to title

// Place this in function.php
function search_by_title( $search, $wp_query ) {
    if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) {
        global $wpdb;

        $q = $wp_query->query_vars;
        $n = ! empty( $q['exact'] ) ? '' : '%';

        $search = array();

        foreach ( ( array ) $q['search_terms'] as $term )
            $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );

        if ( ! is_user_logged_in() )
            $search[] = "$wpdb->posts.post_password = ''";

        $search = ' AND ' . implode( ' AND ', $search );
    }

    return $search;
}


$args = array(
    's' => 'search string',
    'numberposts' => 5,
    'offset' => 0,
    'category' => 0,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'suppress_filters' => true);

add_filter( 'posts_search', 'search_by_title', 10, 2 );
$recent_posts = wp_get_recent_posts($args, ARRAY_A);
remove_filter( 'posts_search', 'search_by_title', 500 );
Comment

PREVIOUS NEXT
Code Example
Php :: get custom field post wordpress dev 
Php :: php while select query 
Php :: what is php artisan 
Php :: debugger in laravel 
Php :: php ref parameter 
Php :: php signature capture 
Php :: phpspreadsheet select sheet 
Php :: php draw line pixel 
Php :: php absint 
Php :: html windows logo 
Php :: laravel return from db reorder 
Php :: php division 
Php :: PDO::ATTR_EMULATE_PREPARES = true Security issue 
Php :: php fpdf in phpmailer 
Php :: php use curl 
Php :: decrypt md5 php 
Php :: Laravel unique with Validation with multiple input value 
Php :: test in laravel 
Php :: get month name php 
Php :: php remove html tag wrap 
Php :: laravel route limit parameter 
Php :: Fetch pivot data laravel 
Php :: month php written out 
Php :: Passing values to blade using redirect() and back() functions 
Php :: laravel toastr option 
Php :: png to pdf 
Php :: array map php 
Php :: laravel where json array column 
Php :: show date php by letters 
Php :: if certain condition is met exit if block php 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =