How to search posts by title with special characters in WP_Query?
I have built a custom query for a website where I have a custom post type named "Program" (i.e).
On this custom post type, let's say I have the taxonomy "Level" where I have some choices. The way I have built the code, you can create as many page with the same title but with different taxonomies. On the frontend, I use a custom WP_Query to fetch all posts with the same title and display a menu to link them all based on the "Levels".
But the problem is that the customer can enter a title with special caracters as '
like Custom's title
.
My request looks like this.
wp_reset_postdata();
// WP_Query arguments
$args = array(
'post_type' = 'program',
'title' = esc_sql(get_the_title()),
'orderby' = 'ID',
'order' = 'ASC',
'tax_query' = array(
'relation' = 'AND',
array(
'taxonomy' = 'level',
'field' = 'slug',
'terms' = array('level-one', 'level-two', 'level-three'),
)
)
);
// The Query
$breadcrumb = new WP_Query( $args );
Everything is fine when the query search by a title without '
. It breaks when I try to search by a title with '
.
Looks like this break the SQL query resulting with a 0 posts count.
Is there a way to create a kind of filter that can say %LIKE% where I could just take a part of the title or simply something else that can bypass the '
?
Note : I can't use the name
parameter (https://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter) because as you can see, I have the same title for many posts so the slug looks like this
my-program-post
my-program-post-2
my-program-post-3
The slug does not border me since is for an internal usage.
Thank you in advance. I will keep searching for a work arround in the meantime.
EDIT 1 : Aditionnal details If you take a closer look to the generated query, I have this.
SELECT SQL_CALC_FOUND_ROWS ri_posts.* FROM ri_posts LEFT JOIN ri_term_relationships ON (ri_posts.ID = ri_term_relationships.object_id) WHERE 1=1 AND ri_posts.post_title = 'Domaine de l’univers social' AND (
ri_term_relationships.term_taxonomy_id IN (5,6,7)
) AND ri_posts.post_type = 'discipline' AND (ri_posts.post_status = 'publish' OR ri_posts.post_status = 'acf-disabled' OR ri_posts.post_status = 'private') GROUP BY ri_posts.ID ORDER BY ri_posts.ID ASC LIMIT 0, 999
As you can see, it looks like WordPress changed my '
to ’
so I get 0 posts from my query.
If I manually change it to :
SELECT SQL_CALC_FOUND_ROWS ri_posts.* FROM ri_posts LEFT JOIN ri_term_relationships ON (ri_posts.ID = ri_term_relationships.object_id) WHERE 1=1 AND ri_posts.post_title = "Domaine de l'univers social" AND (
ri_term_relationships.term_taxonomy_id IN (5,6,7)
) AND ri_posts.post_type = 'discipline' AND (ri_posts.post_status = 'publish' OR ri_posts.post_status = 'acf-disabled' OR ri_posts.post_status = 'private') GROUP BY ri_posts.ID ORDER BY ri_posts.ID ASC LIMIT 0, 999
it works.
I also tried to create %LIKE% filters or str_replace
function but nothing works. It always returns this ’
caracter.
Topic get-the-title wp-query Wordpress
Category Web