WP rest API get post by title

I’m trying to check by a post title if post exist or not.
For some reason when i try something like:

http://domain.com/wp-json/wp/v2/posts?filter[post-title]=table

I want if post with the name table exist, to get the post, if not, get an empty response. But for some reason when post with title not exist i get all posts back.
How could get empty when no post exist and the post when it exist.

Related posts

3 comments

  1. You cannot do this through wp json, please copy the following code in the functions.php:

    function __search_by_title_only( $search, &$wp_query ) {
        global $wpdb;
    
        if ( empty( $search ) )
            return $search; // skip processing - no search term in query
    
        $q = $wp_query->query_vars;    
        $n = ! empty( $q['exact'] ) ? '' : '%';
    
        $search =
        $searchand = '';
    
        foreach ( (array) $q['search_terms'] as $term ) {
            $term = esc_sql( like_escape( $term ) );
            $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
            $searchand = ' AND ';
        }
    
        if ( ! empty( $search ) ) {
            $search = " AND ({$search}) ";
            if ( ! is_user_logged_in() )
                $search .= " AND ($wpdb->posts.post_password = '') ";
        }
    
        return $search;
    }
    add_filter( 'posts_search', '__search_by_title_only', 500, 2 );
    

Comments are closed.