Support

Account

Home Forums Bug Reports The WP 4.8.3 update broke the query on sub field values

Solving

The WP 4.8.3 update broke the query on sub field values

  • After the update of WordPress to the version 4.8.3 the code at section 4. (Sub custom field values) at this page: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ is broken.

    $the_query->have_posts() returns false now.

  • Inspecting the $the_query I noticed a difference on the select. Previously was something like this:
    cnfn_postmeta.meta_key = \'locations_%_city\'

    but now it looks like:
    cnfn_postmeta.meta_key = \'locations_{f12d517a894ce203cd99a63423903c99584233ad4535dc2d10f20cb2029ad0e7}_city\'

    So… removing the % on the filter I fixed the bug (that means now $the_query->have_posts() returns posts) but I’m not sure is a real fix.

    old code:
    $where = str_replace("meta_key = 'locations_%", "meta_key LIKE 'locations_%", $where);

    new code:
    $where = str_replace("meta_key = 'locations_", "meta_key LIKE 'locations_", $where);

    Please, someone more expert can confirm if it’s correct?

  • Hi !
    I have a same bug !
    if I delete % it does not work either.
    what would be the good issue
    thx

    $date = DateTime::createFromFormat(‘Ymd’, get_field(‘date’));
    $args = array(
    ‘post_type’ => ‘programmation’,
    ‘posts_per_page’ => 1,
    ‘order’ => ‘ASC’,
    ‘orderby’ => ‘meta_value’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘dates_%_date’,
    ‘value’ => date(‘Ymd’),
    ‘compare’ => ‘>=’,
    )
    )
    );
    //var_dump($args);
    $query = new WP_Query( $args );

  • Try to remove the % only on the filter here:
    $where = str_replace("meta_key = 'dates_", "meta_key LIKE 'dates_", $where);

    Can you execute the following and then send to us after $query?
    <?php echo '<pre>'; var_export($the_query); echo '</pre>'; ?>

  • it’s ok !!!!!!!
    Thank you so much !!!

  • Same issue here since the 4.8.3 update, except I have not found a workaround.

        $args = shortcode_atts( array(
    		'post_type' => 'my_menu',
    		'post_status' => 'publish',
    		'posts_per_page' => '-1',
    		'orderby' => 'menu_order',
    		'order' => 'ASC',
    		'season' => '',
    		'menu' => '',
    		'section' => '',
    		'section_title' => '',
    		'subtitle' => '',
    		'meta_query' => '',
        ), $atts );
        
        // Get any parameters from the shortcode call
        $season = $args['season'];
        $menu = $args['menu'];
        $section = $args['section'];
        $section_title = $args['section_title'];
        $subtitle = $args['subtitle'];
    
        // Add query args based on ACF
    	if ( function_exists('get_field') ) {
    	    // Season (checkbox)
    	    $season_array = '';
    		if ( $season != '' ) {
    			$season_array = array(
    				'key'		=> 'season',
    				'value'		=> $season,
    				'compare'	=> 'LIKE'
    			);
    		}
    
    	    // Menu (repeater)
    	    $menu_array = '';
    		if ( $menu != '' ) {
    			$menu_array = array(
    				'key'		=> 'menu_repeater_%_menu',
    				'value'		=> $menu,
    				'compare'	=> 'LIKE'
    			);
    		}
    		
    	    // Section (checkbox)
    	    $section_array = '';
    		if ( $section != '' ) {
    			$section_array = array(
    				'key'		=> 'section',
    				'value'		=> $section,
    				'compare'	=> 'LIKE'
    			);
    		}
    		
    		// Output meta_query args
    		$meta_query = array(
    			'relation' => 'AND',
    			$season_array,
    			$menu_array,
    			$section_array,
    		);
    
    		$args['meta_query'] = $meta_query;
    	}

    If I remove $menu_array from the $meta_query array, the query works (but retrieves more posts than I want, obviously). I have tested different keys for $menu_array, but nothing works.

  • Update: Found a workaround on https://make.wordpress.org/core/2017/10/31/changed-behaviour-of-esc_sql-in-wordpress-4-8-3/#comment-33136 (adjusted to match my key)

    //fix WP 4.8.3 bug with repeater
    function my_posts_where( $where ) {
        global $wpdb;
        $where = str_replace(
                  "meta_key = 'menu_repeater_%_menu", 
                  "meta_key LIKE 'menu_repeater_%_menu",
                  $wpdb->remove_placeholder_escape($where)
        );
        return $where;
    }
  • Thanks for the pointer in Eric’s version ^ the add_filter line is missing;
    add_filter(‘posts_where’, ‘my_posts_where’);

    Otherwise, works like a charm, just wait till your WP is 4.8.3 to implement as it’ll kill 4.8.2 and lower.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘The WP 4.8.3 update broke the query on sub field values’ is closed to new replies.