• Resolved bigflannel

    (@bigflannel)


    I have run into a tricky issue.

    On a page I am building I am using WordPress WP_Query to compare a custom post type’s ACF field with a custom taxonomy term. This are my WP_Query args:

    $args = array(
    	'post_type' => 'photographs',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'subjects',
    			'field'    => 'name',
    			'terms'    => get_field( 'subject_first_name', get_the_ID() ) . ' ' . get_field( 'subject_last_name', get_the_ID() ),
    		),
    	),
    	'meta_query' => array(
    	    'photo_date' => array(
    	    	'key' => 'photo_date',
    	    	'type'    => 'NUMERIC',
    	    	'compare' => 'EXISTS',
    	    ),
    	),
    	'orderby' => array(
    		'photo_date' => 'ASC',
    	), 
    	'posts_per_page' => -1,
    );

    The code works fine until the text I am comparing includes an apostrophe. For example, when the text is Georgia O’Keeffe.

    ACF get_field returns the text with the apostrophe converted to an HTML Character Entity &#039 ;. The custom taxonomy appears to return the text with an actual apostrophe. I do not find a match (even though there are three).

    I have tried using the following on the ACF get_field return:

    esc_sql
    $wpdb->prepare
    html_entity_decode

    before comparing it with the custom taxonomy, nothing seems to help.

    I am a little lost. Any pointers, thoughts, help … magic silver bullets … greatfully appreciated.

    Regards
    Mike

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    html_entity_decode() alone should restore the actual plain text term that you can use in constructing a query. WP will add slashes when composing the query, but you don’t need to be concerned about that part, it’s supposed to be handled internally.

    I believe you’ve stumbled upon a known bug (Trac 39315). It’s slated to be patched in 4.8. Until then you can apply the patch to your own code base.

    While waiting for the approved patch to be developed, I made up the following work around (different than the official patch) inserted in the foreach loop at line 620 (/wp-includes/class-wp-tax-query.php):

    //potential patch for 4.8 prevents double escaping
    //https://core.trac.www.ads-software.com/ticket/39315#comment:1
    $clean_term = sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' ); 
    
    // Match sanitation in wp_insert_term(). 
    $clean_term = wp_unslash( $clean_term ); 
    
    $term = "'" . esc_sql( $clean_term ) . "'";
    //------------------------------------------------------

    This is the one situation when it’s OK to edit core files because when the official patch is released it will overwrite the altered file, deleting my code and replacing it with the official, bug free version.

    Thread Starter bigflannel

    (@bigflannel)

    @bcworkz … I cannot thank you enough. Adding this code solved my problem.

    I broadly understand what I am doing when I implement your fix, but it is a level of detail I am not familiar with.

    This is the second time you have come to my aid in the last few years when I am facing a problem and am at a loss. Thank you. It is truly appreciated and makes the world seem like a better place.

    Regards
    Mike

    Moderator bcworkz

    (@bcworkz)

    Ah shucks. What a nice thing to say!

    The world needs to be a better place. I’m glad I’m able to add to that in some tiny way. You’re welcome ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WP_Query, Custom Taxonomy, Custom Field Comparison’ is closed to new replies.