1

I have been searching for hours for a solution for my problem. I couldn't find anything quite like what I am trying to achieve., so I am really stuck and appreciate any help!

I have a CPT called "magazine", with several ACF fields. One ACF field is the "year" the magazine was published. Another field is the "magazine number", which is like an unique ID. Each year several magazines are published.

What I am trying to achieve is an output, that gives me a "ul" element for every "year" and "li" elements for every "magazine number", that was published in the corresponding "year".

For exmaple like this:

<ul class="2019">
   <li>200</li>
   <li>199</li>
   <li>198</li>
   <li>197</li>
   <li>196</li>
</ul>

<ul class="2018">
   <li>195</li>
   <li>194</li>
   <li>193</li>
   <li>192</li>
   <li>191</li>
</ul>

I have no idea, logic wise, how to approach this problem. How can I cross reference the fields, reduce all "year" field (which are several) to one output, and then output all "magazine number" published in the specific year and then output several lists for each year, as shown above?

JMan
  • 11
  • 3
  • Where are you trying to output this information? Is this on an archive? – disinfor Oct 14 '19 at 17:56
  • I was trying to output on a single-magazin page, a bit later I can add some code from how I got the "years" list working, then I need a second column for all "magazine numbers" when I click on the year (which I am going to do with jQuery). – JMan Oct 14 '19 at 18:17

2 Answers2

0

You might try something like this. To me, it seems you need to get all the years first, then run queries for each year to get the magazine numbers. I'm not certain how intensive this would be on your server, you'd have to just give it a try. May have to adjust for your field names.

<?php

global $post;

$args = array(

   'post_type' => 'magazine'

);

$posts = new WP_Query($args); // Query posts of 'magazine' post type

$magazine_years = array(); // set up array to put years in

if ($posts) {

   foreach ($posts as $post) {

      setup_postdata( $post );

      $the_year = get_field('year'); // get the value of year field
      $magazine_years[] = $the_year; // add year to the array

   }


}

wp_reset_postdata(); // reset the query so we dont introduce a problem with more queries

foreach ($magazine_years as $year) {

// do a query for each year

   $args = array(

      'post_type' => 'magazine',
      'meta_key' => 'year',
      'meta_value_num' => $year

   );

   $posts = new WP_Query($args);

   if ($posts) { ?>

  <!-- create your list -->

    <h1><?php echo $year; ?></h1>
    <ul class="<?php echo $year; ?>">

<?php foreach($posts as $post) {

         setup_postdata( $post );
         <li><php the_field('magazine_number'); ?></li>

      } ?>

    </ul>

<?php   

  }

 // reset the query data so you can run another without issue

  wp_reset_postdata();

}
DubVader
  • 1,032
  • 1
  • 6
  • 8
  • thank you very much for your input I have tried following code and it does not work, I get a very long list with the same magazine number over and over for every 'li' element. – JMan Oct 15 '19 at 08:57
  • Interesting. In that case it seems like a slight adjustment is needed, but that this is the general idea that you need to accomplish what you're looking for. We might just be missing setting up the post data. I've made an adjustment to the original solution. Does it appear by the number of li elements you see, that the total li elements corresponds to the total number of posts? – DubVader Oct 15 '19 at 15:37
  • When I was trying your code, I also had quite a few empty 'lis' and way too many of the same item, so there were alot more than the total number of posts. – JMan Oct 15 '19 at 17:02
  • Also, do you have any idea how I could save those values in a nested array like `2018 => array(156, 155, 154, 153)`? – JMan Oct 15 '19 at 17:03
  • Yes. Create an empty array to put your years and ids in. If you put all the IDs for the year in their own array during the loop, after the loop is over you can pass the year and its ids to the pre-created array. Here's an example: https://repl.it/@dubvader/PHP-Testing-Envo – DubVader Oct 15 '19 at 17:44
  • Thank you for the answer. So, I would use a WP query too loop trough all years, save them into an array, then loop trough the mag-numbers and save them into an array and then save the mag array into the year array? – JMan Oct 15 '19 at 19:32
  • Sort of. Create the year array first. Create another array for you final array. We'll call it ```$mag_by_year = array();``` Then loop through the years array, and do a query (you can just use get_posts(); for this) for mags in each year. Loop through the mags, grab the info you need, put in the final array with the year in the first loop something like ```$mag_by_year[$year] = $whatever_data_you_need```. – DubVader Oct 15 '19 at 21:46
  • Interestingly looking at my initial post and the last I'd probably say you might be better of just using get_posts() instead of queries. Up to you though. Good luck – DubVader Oct 15 '19 at 21:49
  • Thank you very much for your help. It really helped me a lot, as I am still kinda new to php. – JMan Oct 16 '19 at 12:23
0

I managed to get the desired reults with some help from your input @DubVader

Here is the code I used:

<?php foreach($magazine_year as $option ){

    $args = array(

        'post_type' => 'magazine',
        'meta_key' => 'year',
        'meta_value' => $option

     );

$the_query = new WP_Query( $args ); ?>

<?php if( $the_query->have_posts() ):

     ?>
    <ul class="<?php echo $option; ?>">
    <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
                <?php the_field('magazine_number'); ?>
        </li>
    <?php endwhile; ?>
    </ul>
<?php endif; }?>

<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>    
JMan
  • 11
  • 3