Support » Fixing WordPress » WP query doesn’t work as expected with PHP8 when used in AJAX callback

  • Hi,

    WP query doesn’t seem working as expected with PHP8 when it’s used in an AJAX callback function.

    Both with WP_Query and get_posts, using $post->ID or get_the_ID() within the foreach following the query returns NULL.

    I tested multiple scenarios, w/wo wp_setup_postdata, $wpdb->get_posts, etc., and the result was always the same.

    Thanks for investigating this further…

Viewing 5 replies - 1 through 5 (of 5 total)
  • Been there. Even WP5.9 is not fully compat with PHP 8+ (yet, soon, I hope).

    Have you tried PHP 7.4X?

    Info: https://make.wordpress.org/core/2022/01/10/wordpress-5-9-and-php-8-0-8-1/

    Thread Starter Gabor Lippert

    (@lunule)

    Hi @swansonphotos, thanks for the follow-up and for the official announcement link.

    And yes, it works as expected with PHP 7.4 – problem is, the client wants PHP 8.
    But I’m happy to see that the fact that WP5.9 is not fully compat with PHP 8+ is an official information – this is something I can use to persuade him to stay with 7.4 for now.

    Thread Starter Gabor Lippert

    (@lunule)

    Quick update – I’m resetting this to ‘not resolved’.

    The reason why I think this ticket should remain open is that the information in the official announcement (and the potential temporary fixes/polyfills) doesn’t include/cover anything about the specific problem reported here.

    The get_posts query I use in the AJAX callback function is totally by the book – yet it totally doesn’t work. AND it starts working as expected as soon as it’s tested anywhere else but in an AKAX callback function.

    Thread Starter Gabor Lippert

    (@lunule)

    I’m attaching the query I use, maybe/hopefully someone notices something I didn’t:

    $mealtype_mq_Arr = array(
    	'relation' => 'OR',
        array(
            'key'     => 'kmp-cf-mealtype',
            'value'   => 'breakfast',
            'compare' => 'LIKE',
        ),
        array(
            'key'     => 'kmp-cf-mealtype',
            'value'   => 'lunch',
            'compare' => 'LIKE',
        ),
        array(
            'key'     => 'kmp-cf-mealtype',
            'value'   => 'dinner',
            'compare' => 'LIKE',
        ),
    );
    
    // + similar definitions of the $diettype_mq_Arr, $sensitivity_mq_Arr, 
    //   $complexity_mq_Arr arrays.	    
    // (...)    
    
    $query_args_Arr = array(
    	'post_status' 		=> 'publish',
    	'post_type' 		=> 'kmp-meal',
    	'posts_per_page' 	=> -1,
    	'meta_query'		=> array(
            'relation' => 'AND',
    		$mealtype_mq_Arr,
    		$diettype_mq_Arr,
    		$sensitivity_mq_Arr,
    		$complexity_mq_Arr,	        
    	 	)
    );
    
    $posts_Arr 	= get_posts( $query_args_Arr );
    
    foreach ( $posts_Arr as $post ) :
    
    	$pid = $post->ID;
    	// (...)
    
    endforeach;
    • This reply was modified 6 months, 2 weeks ago by Gabor Lippert.
    Thread Starter Gabor Lippert

    (@lunule)

    👍 Solved.

    With the latest WP version, I can’t replicate the issue anymore, meaning the core WP dev team fixed the bug.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.