Dariusz
Dariusz

Reputation: 303

Not displaying all categories with term meta value

I need to display the categories with the value of 2 for the term meta. Below is my code:

  <?php
        $args = array(
            'show_option_all'    => '',
            'orderby'            => 'ID',
            'order'              => 'ASC',
            'posts_per_page'     => -1,
            'meta_query' => array(
               array(
                  'key'       => 'term_category_realized',
                  'value'     => 2,
                  'compare'   => '==='
               )
             )
        );

        $categories = get_categories($args);



        echo '<ul class="realized-projects-category-list">';
          foreach($categories as $category) {
             echo '<li>';
              echo '<a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a>';
             echo '</li>';
          }
        echo '</ul>';
      ?>

The problem with this script is that I have 9 entries in the database that fit to the query but it's only displaying 4 of them.

Does anybody know what am I missing in my code?

Upvotes: 0

Views: 55

Answers (1)

dineshkashera
dineshkashera

Reputation: 1502

To get term by term meta values, Please try the get_terms using the $args parameters.

See the Below code :

 $args = array(
      'taxonomy' => 'YOUR-TAXONOMY-NAME',
      'orderby' => 'ID',
      'order' => 'ASC',
      'hide_empty' => false,
      'posts_per_page'     => -1,
      'meta_query' => array(array(
        'key' => 'term_category_realized',
        'value' => 2,
        'compare' => '='
      )),
    );

    $terms = get_terms( $args );
  print_r($terms);

For more help : Click Here

Upvotes: 2

Related Questions