O
O
OlegSedoy2018-07-23 12:43:05
WordPress
OlegSedoy, 2018-07-23 12:43:05

How to create an array of cookies?

I write a string to the cookie:

$post_visible = implode(",", $posts);
      setcookie('visible_posts', $post_visible, time()+3600, '/');

Then, I check if there is such an array and make a request:
<?php get_header(); ?> 
<div class="grid">
  <div class="grid-sizer"></div>
  <?php
  if (isset($_COOKIE['visible_posts'])){

    $data = explode(',',$_COOKIE['visible_posts']);
    $post_in = [];
    foreach ($data as $value) {
      array_push($post_in, intval($value));
    }

    $args = array(
      'post__in'	=> 	$post_in,
      'order'           => 'DESC',
      'orderby'         => 'date',
    );
  } else {
    $args = array(
      'posts_per_page'	=> 10,
      'order'           => 'DESC',
      'orderby'         => 'date',
    );
  }
  

  $query = new WP_Query($args);
  ?>
  <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
    <?php get_template_part('loop'); ?>
    <?php
  endwhile;
endif;
?>	 
</div>
<?php get_template_part('assets/parts/load','more'); ?>
<?php get_footer(); ?>

Everything seems to be correct, but it does not display a list of posts from the $post_in array. Where can be an error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OlegSedoy, 2018-07-23
@OlegSedoy

Figured it out, maybe it will be useful for someone:

$args = array(
      'post__in'	=>	$post_in,
      'order'           => 'DESC',
      'orderby'       => 'date',
      'nopaging'    => true,
    );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question