10

I'm using the WP REST API v2 to try and search for all posts with the same title, what I'm trying to do is create a sort of head-to-head/previous meetings page for 2 teams.

At the moment i can retrieve a single post with a slug no problem

.../wp-json/sportspress/v2/events?slug=team1-vs-team2

When i use a search it retrieves all the events with team1 and team2, but also all references to team1 & 2 from post content which is not what I want...

.../wp-json/sportspress/v2/events?search=team1+team2

How do i retrieve a post using the search using the exact post title as shown below in title > rendered ??

0   
  id    60455
  date  "2016-11-22T19:30:00"
  date_gmt  "2016-11-22T19:30:00"
  modified  "2016-11-23T09:25:29"
  modified_gmt  "2016-11-23T07:25:29"
  slug  "team1-vs-team2"
  status    "publish"
  type  "sp_event"
  link  ".../event/team1-vs-team2/"
  title 
      rendered  "Team1 vs Team2"
1
  • Any update to this?
    – Luple
    Mar 10 '20 at 17:57
0

Unfortunately, it is not possible, but you can use slug instead.

Like this:

http://example.com/wp-json/wp/v2/posts?slug=table

Reference document URL are: https://developer.wordpress.org/rest-api/reference/pages/#arguments

3
  • It must be possible to add post title as a searchable field using a filter?
    – Rup
    Sep 6 at 8:09
  • No, as per our knowledge it's not possible. for the post title, you have to create a custom search. but you can search from post slug. Note: most of case post slug and title both are same, so if you have only post title and you want to search with the post title, you can replace title space to - and convert title to lower case it's will same as post slug(most of case) Sep 6 at 8:31
  • Example: $title = 'Terms and Conditions'; $delimiter = '-' $slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $title))))), $delimiter)); Sep 6 at 8:34
0

Of course it's possible! You'll just need to add in a custom endpoint for the REST API.

To do that, drop the code below into your functions.php (or better yet, a plugin so it's not tied to your theme).

First, register the custom route and allow it to take a "title" parameter.

/**
 * Register the custom route
 *
 */
function custom_register_your_post_route() {
    register_rest_route( 'custom-search/v1', '/posts/(?P<title>.+)', array(
        array(
            'methods'  => WP_REST_Server::READABLE,
            'callback' => 'custom_get_post_sample'
        )
    ) );
} 
add_action( 'rest_api_init', 'custom_register_your_post_route' );

Next, add in the custom callback function to find and return the posts you're looking for, using the built-in WP_REST_Posts_Controller.

/**
 * Grab all posts with a specific title
 *
 * @param WP_REST_Request $request Current request
 */
function custom_get_post_sample( $request ) {
    global $wpdb;

    // params
    $post_title = $request->get_param( 'title' );
    $post_type = 'post';

    // get all of the post ids with a title that matches our parameter
    $id_results = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", urldecode( $post_title ), $post_type ) );
    if ( empty( $id_results ) ) {
        return rest_ensure_response( $request );
    }
    
    // format the ids into an array
    $post_ids = [];
    foreach( $id_results as $id ) {
        $post_ids[] = $id->ID;
    }

    // grab all of the post objects
    $args = array(
        'post_type' => $post_type,
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'post__in' => $post_ids
    );
    $posts = get_posts( $args );
 
    // prepare the API response
    $data = array(); 
    $rest_posts_controller = new WP_REST_Posts_Controller( $post_type );
    foreach ( $posts as $post ) {       
        $response = $rest_posts_controller->prepare_item_for_response( $post, $request );
        $data[] = $rest_posts_controller->prepare_response_for_collection( $response );
    }
 
    // Return all of our post response data
    return rest_ensure_response( $data );
}

This will give you a response that looks just like the built-in Posts endpoint, including any additional data added by plugins (Yoast SEO for example).

There's plenty of additional documentation under the Extending the REST API section at wordpress.org if you need more functionality.

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.