2

If I want to find all posts where the very first character(s) of the post text / content (ie not the title) are

"<" 

or

"<a"

How would I do so?

I tried these solutions:

https://stackoverflow.com/questions/2246263/fetch-posts-starting-with-alphabet-x

https://stackoverflow.com/questions/39205088/querying-post-id-from-posts-starting-with-specific-character

but they did not work

3

Something like this will probably work:

$results = array();

$allPosts = get_posts('post_type=post&numberposts=-1');
foreach ($allPosts as $aPost) {
    if ( substr($aPost->post_content, 0, 1) == '<' || substr($aPost->post_content, 0, 2) == '<a' )
        $results[] = $aPost->ID;
}

echo "<pre>".print_r($results,true)."</pre>";

UPDATE: but you should revisit the MySQL approach in the links you provided. This php method is somewhat wasteful as it gets everything, instead of only whats needed.

2
  • 1
    Wouldn't it be better to do this with simple MySQL query though? Selecting all posts and looping through them isn't very efficient approach, I guess. Mar 22 '18 at 12:55
  • 1
    you're right, mysql directly would be less waste. The links in the OP were using MySQL, for the same problem, and they didn't work for him - I just did a php alternative. I'll throw up a note about that. Mar 22 '18 at 16:06

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.