Search code examples
wordpressgettitleposts

WordPress get_posts by title like


I am trying to create a small search-function for WordPress. An AJAX call should get all posts where the title is like %quote%.

Is there a possibility to make this happen inside the get_posts() function?

Don't get me wrong. The ajax works fine. I have the ajax function in my functions.php and I receive the posts. It's just the "where title like" part where I couldn't find a solution.


Solution

  • No, but you can create a custom loop.

    Check this.

    EDIT:

    $args = array('s' => 'keyword');
    
    $the_query = new WP_Query( $args );
    
    // The Loop
    if ( $the_query->have_posts() ) {
        
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            //whatever you want to do with each post
        }
    } else {
         // no posts found
    }