Hello all,
I would like to query the 3 latest posts that include the phrase “help needed”.
Anyone knows how can i achive this ?
The solutions below are for Query editor (PHP)
If your posts content is stored in the default post_content table (e.g. gutenberg), this below should work:
return [
's' => 'help needed',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC'
];
bricks stores it’s content in the postmeta table as a json object so you need to do a meta_query for the “_bricks_page_content_2” key.
This worked for my rudimentary testing:
return [
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => [
[
'key' => '_bricks_page_content_2',
'value' => 'help needed',
'compare' => 'LIKE',
],
],
'post__not_in' => [get_the_ID()] //Exclude current Post*
];
* Since the string “help needed” is now inside this posts _bricks_page_content_2
postmeta?, it will also be shown(if you’re querring for its post_type). So it might be best to exclude the current Post.
Also the regular ‘s’ parameter has a more robust search. Although the bricks meta_query disregards upper and lowercase, if you write e.g. “helpneeded” as one word, it won’t find it.
So if you want to make it more robust you might do this:
return [
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => [get_the_ID()], // Exclude current post
'meta_query' => [
'relation' => 'OR', // Use 'OR' to match any of the phrases
[
'key' => '_bricks_page_content_2',
'value' => 'help needed',
'compare' => 'LIKE',
],
[
'key' => '_bricks_page_content_2',
'value' => 'helpneeded',
'compare' => 'LIKE',
],
[
'key' => '_bricks_page_content_2',
'value' => 'help need',
'compare' => 'LIKE',
],
],
];
Hope this Helped
Cheers Suat
Thank you so much for the effort Suat, but its not working for me, how hard i tried to set it up in my case.