Support

Account

Home Forums General Issues Show number of objects in each query

Helping

Show number of objects in each query

  • Don’t know how to describe to correctly but i will give it a shoot.

    Am using following code will give me a list like
    Android
    iPhone
    Blackberry

    <?php 
    // args
    $args = array(
    	'posts_per_page' => -1,
    	'post_type' => 'bonus',
    	'meta_query' => array(
            array(
                'key' => 'listing_type', // name of custom field
                'value' => '"Mobile"', // matches exaclty "red", not just red. This prevents a match for "acquired"
                'compare' => 'LIKE'
            ))
    );
    // get results
    $the_query = new WP_Query( $args );
     
    // The Loop
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    	
    <a class="list-group-item" href="<?php the_permalink(); ?>" title="">
      
      <?php the_title();?>
    
      </a>
    
    <?php endwhile; ?>
    	
        <?php endif; ?>
      <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?> 

    Is there any way that i can show the number of objects in each of this ? like.. “Android 15”

    For example on the android page i would use the following query to fetch my items.

    $type = casino;
    $key = mobile_platforms;
    $value = Android;
    
    // args
    $args = array(
    	'meta_key' => 'votes_count',
    	'orderby' => 'meta_value_num',
    	'order' => 'DESC',
    	'posts_per_page' => -1,
    	'post_type' => $type,
    	'meta_query' => array(
            array(
                'key' => $key, // name of custom field
                'value' => $value, // matches exaclty "red", not just red. This prevents a match for "acquired"
                'compare' => 'LIKE'
            ))
    		);

    Any idea ?

  • Hi @mindgames

    Your first query will find all mobiles, correct?
    If you want to list the total mobiles in each category, you can simply loop over your results and load the mobile category.

    Then use this category to increase a counter like so:

    
    $counter = array(
    'android' => 0
    );
    
    // start loop over posts
    $category = 'xxx';
    $counter[ $category ]++;
    // end loop over posts
    
    //output
    echo 'android total = ' . $counter['android'];
    

    Hope that helps.

    Thanks
    E

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Show number of objects in each query’ is closed to new replies.