0

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.

  • It's unclear where does user enter the title. – Max Yudin Dec 19 '18 at 16:21
  • You should use esc_sql when you're preparing some SQL statement. You shouldn't use it in this case. You don't have to escape values passed to WP_Query. – Krzysiek Dróżdż Dec 19 '18 at 19:15
  • The problem is that it looks like WordPress if formating my string with when I typed '. So I have no results since it is not the same caracters? – Patrice Poliquin Jan 16 '19 at 21:29
  • Found the problem. Looks like the person who enter the title copy-pasted from a special keyword or a strange text editor. – Patrice Poliquin Jan 24 '19 at 21:10
0

Their is no real issue to fix this. After some researchs and test, I figured out that the way the person copy-pasted or wrote the titles was wrong with the '

Ciel d<del>’</del>Afrique et pattes de gazelle

Versus the good way

Ciel d<del>'</del>Afrique et pattes de gazelle

Closing this tread.

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.