Using Cookie Data For WP_Query Loop

You need to explode the comma separated string to get the array of IDs. Follow the below code.

$cookie_array = $_COOKIE["your-results"];

// Test output              
echo $cookie_array;

$cookie_array = array_map( 'absint', (array) explode(',', $cookie_array) );

$sug_args =  array(
  'post_type' => 'product',
  'post__in'  => $cookie_array,
);

$sug_query = new WP_query ($sug_args);

if( $sug_query->have_posts() ) : 
    while( $sug_query->have_posts() ) : 
        $sug_query->the_post();

        // Run Loop                   
        wc_get_template_part( 'content', 'product' ); 

    endwhile; 
endif; 

wp_reset_query();

Leave a Comment