Sage adds weird strings to WP_Query()

I’m basically creating a search function, but my query always returns empty even though the search string matches a post in my database.
image
image

And I noticed when I dumped the WP_Query, it seems like there are some weird (hashed) strings included in the actual MySQL query.

which makes me think as to why the query is failing

This doesn’t help with the issue of your search not returning any results but those characters are a way for WP to prevent SQL injection and has nothing to do with Sage.

https://make.wordpress.org/core/2017/10/31/changed-behaviour-of-esc_sql-in-wordpress-4-8-3/

1 Like

Not really sure how you’re looping WP_Query in this context but I’d probably just use get_posts() depending on the situation.

/**
 * Search for posts containing the specified keyword.
 *
 * @param  string $value
 * @return array
 */
public function search($value = '')
{
    if (empty($value)) {
        return [];
    }

    $value = sanitize_text_field($value);

    $results = collect(
        get_posts([
            'post_type' => ['post'],
            'post_status' => 'publish',
            'posts_per_page' => 25,
            's' => $value,
        ])
    )->filter();

    if ($results->isEmpty()) {
        return [];
    }

    return [
        'count' => $results->count(),
        'items' => $results->take(5)->map(function ($post) {
            return [
                'id' => $post->ID,
                'title' => get_the_title($post->ID),
                'url' => get_permalink($post->ID),
                'category' => collect(
                    get_the_category($post->ID)
                )->shift()->name
            ];
        })->toArray()
    ];
}
1 Like

I’m using the

array_map( function ($post) {
}, $query->posts)