user3354912 - 8 months ago 39
PHP Question

# WordPress: applying str_replace to WP_Query search results

I'm trying to get all the posts that contain current post's title in their content.

And I have almost achieved that, except for the one thing: for posts that contain the apostrophe or the right quotation mark in their title it returns no result. While posts that contain the current search term with the apostrophe or the right quotation marks are definitely exist.

Here is the code:

$title = '"'.str_replace(array("'","’","&#39;","&#8217;"), array("","","",""), wp_strip_all_tags(get_the_title())).'"';$args = array(
's'                 => $title, 'cat' => 48, 'posts_per_page' => -1, 'orderby' => date, 'order' => desc, );$query = new WP_Query( $args ); if ($query->have_posts() ) {
while ( $query->have_posts() ) {$query->the_post();
echo the_time("Y.m.d");
echo ': ';
echo the_title();
echo '<br />';
}
}


So my idea was to get rid of all the apostrophes and all the right quotation marks in both the search term and the WP_Query search results.

As it can be seen I've successfully (at least I think so) applied
str_replace
to the search term and now it returns

But I totally can not get how to apply the same
str_replace
to the WP_Query results.

As a temporary (but may be the best at the same time) solution I've run the following SQL query

UPDATE wp_posts SET post_content=REPLACE(post_content,'\&\#039\;','\'');
UPDATE wp_posts SET post_content=REPLACE(post_content,'\&\#8217\;','\'');
UPDATE wp_posts SET post_content=REPLACE(post_content,'\’','\'');


to alter my existing posts, and then redundantly changed

$title = '"'.str_replace(array("'","’","&#39;","&#8217;"), array("","","",""), wp_strip_all_tags(get_the_title())).'"';  with $title = '"'.str_replace(array("’","&#39;","&#8217;"),
array("'","'","'"),
wp_strip_all_tags(get_the_title())).'"';


Seems I have achieved the desired result, at least for now.