• frivolio

    (@frivolio)


    I have the following code which test categories and display if they have posts with some metadata set or not.

    <?php
    $category_counter = count(get_the_category());
    $counter = 0;
       foreach((get_the_category()) as $cat)
       {
          $counter++;
          $category_variable = $cat->cat_ID;
          $query_paying_companies = 'cat=' . $category_variable. '&meta_key=field-company-type&meta_value=j';
          query_posts($query_paying_companies); ?>
          <?php if ( have_posts() ) :?>
                <p><?php echo $category_variable;?> - This category is good!</p>
          <?php else: ?>
                <p><?php echo $category_variable;?> - This category is NOT good!</p>
          <?php endif; ?>
    <?php wp_reset_query();
      }
    ?>

    Code works well. What I want – and I do not succeed to complete – is to create and array of “GOOD categories” – only the categories with posts which have this metadata set and then random choose one.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Michael

    (@alchymyth)

    <?php
    $good_categories = array(); //NEW CODE
    $category_counter = count(get_the_category());
    $counter = 0;
       foreach((get_the_category()) as $cat)
       {
          $counter++;
          $category_variable = $cat->cat_ID;
          $query_paying_companies = 'cat=' . $category_variable. '&meta_key=field-company-type&meta_value=j';
          query_posts($query_paying_companies); ?>
          <?php if ( have_posts() ) :?>
                <p><?php echo $category_variable;?> - This category is good!</p>
    <?php $good_categories[] = $category_variable; //NEW CODE ?>
          <?php else: ?>
                <p><?php echo $category_variable;?> - This category is NOT good!</p>
          <?php endif; ?>
    <?php wp_reset_query();
      }
    ?>

    to check your results, you could add this after the code:
    <?php var_dump($good_categories); ?>

    Thread Starter frivolio

    (@frivolio)

    Thanks a lot alchymyth! I managed myself to make something similar. Works like a charm! Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Categories array’ is closed to new replies.