Can WP Query LIKE operator find only posts with the search text at the beginning?

Hello,
first: Thank you for all the great new features!
My question:
Is it possible in a WP query that the LIKE operator finds only posts with the search text at the beginning?
I have read in another thread here in the forum, that the looper provider query string does not support regex - is that right?
Kind regards,
Hannes

Hello Hannes,

Thanks for writing in! You can use an argument something like this:

$search_text = 'your_search_text'; // Replace with your actual search text

$args = array(
    'post_type' => 'post',  // Replace 'post' with your custom post type if needed
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'post_title',  // Replace with the actual post title meta key
            'value' => $search_text,
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'post_content',  // Replace with the actual post content meta key
            'value' => $search_text,
            'compare' => 'LIKE',
        ),
    ),
);

Best Regards.

Thank you.
I have tried this already, it brings me all results where search_text is somewhere in the fields.
I need to find the results where search_text stands only at the beginning of the fields.
In MySQL I would do this:
SELECT * FROM table WHERE field LIKE “search_text%”
Is this possible?
Best regards,
Hannes

Hi Hannes,

That is possible if you use the ^ in front of the key you are searching for. And you also need to use REGEXP as shown in the following thread.

Please remember that the above code will work if copied as it is and don’t conflict with any existing style.
Please note that the code provided serves only as a guide to help you get started custom-coding on your own if there’s no option offered in our theme or the products we bundle.
We really do not provide support for custom codes, which means we can’t fix it in case it conflicts with something on your site, nor will we enhance it. Further customization should be directed to a third-party developer or you can avail of One, where we answer the questions beyond normal theme support.

Thanks

Thank you so much!
Using CS Loopers and WP Query is becoming more and more exciting!

Hi Hannes,

Glad that we are able to help you.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.