• Resolved scottiescotsman

    (@scottiescotsman)


    What I am trying to do is count the amount of custom movie posts a certain custom taxonomy actor is in.
    I can list all the actors fine, but just cant count the amount of movies the actors are in.
    I know I am close but cant get it to work ??

    <?php foreach (get_terms('actor') as $cat) : ?>
    <div class="col-md-4">
    <?php
    $cat = $cat ->name;
    $query = new WP_Query( array( 'movies' => '$cat',
    ) );
    $count = $query->found_posts; ?>
    <?php echo $cat; ?>[<?php echo $count; ?>]
    </div>
    <?php endforeach; ?>
Viewing 14 replies - 1 through 14 (of 14 total)
  • If you are trying to get post (movies) by category name, you need to use following code :

    $query = new WP_Query( array( 'category_name' => $cat ) );
    where $cat is the category slug.

    if separate taxonomy has been created then use following code :

    $args = array(
    	'post_type' => 'movies',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'movie_taxonomy', // taxonomy name
    			'field'    => 'slug',
    			'terms'    => array( $cat ),
    		),
    	),
    );
    $query = new WP_Query( $args );

    This will fetch the movies (posts) that are associated with that particular $cat (category name / actor).

    Let me know if this works.

    Thread Starter scottiescotsman

    (@scottiescotsman)

    I think I will explain it a little better.
    I have custom post type ‘movies’
    custom taxonomy ‘actors’
    I want to list all the ‘actors’ within the db [which I can do] but also add the amount of ‘movies’ that the current ‘actor’ is associated with.

    i.e.

    brad pit [14]

    thanks in advance

    $args = array(
    	'post_type' => 'movies',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'actors',
    			'field'    => 'slug',
    			'terms'    => array( $cat ),
    		),
    	),
    );
    $query = new WP_Query( $args );
    
    $count = $query->found_posts;

    try with this once.

    Thread Starter scottiescotsman

    (@scottiescotsman)

    Recoverable fatal error: Object of class WP_Term could not be converted to string in C:\xampp\apps\wordpress\htdocs\wp-content\themes\SimpleAs\page-actor.php on line 43

    Thread Starter scottiescotsman

    (@scottiescotsman)

    <?php foreach (get_terms('actor') as $cat) : ?>
    
    				<div class="col-md-4">
    				
    				<?php $args = array(
    						'post_type' => 'movies',
    						'tax_query' => array(
    							array(
    								'taxonomy' => 'actors',
    								'field'    => 'slug',
    								'terms'    => array( $cat ),
    							),
    						),
    					);
    					$query = new WP_Query( $args );
    
    					$count = $query->found_posts;			
    
    				?>
    				
    				<?php echo $cat; ?>
    					
    				</div>
    				
    				<?php endforeach; ?>

    If you print $cat you will get an array which contains name. If you are getting that in an object array, you need to pass it as something like this.

    $cat->name;

    Please print the array once to make sure you get term name in name, and then pass above code as a name.

    Hope this helps.

    Thread Starter scottiescotsman

    (@scottiescotsman)

    i changed
    <?php echo $cat->name; ?>[<?php echo $count; ?>]
    lists all actors correctly but movie count = 0 in all.

    Thank you vey much for all your help

    Happy to help you. Is the data displayed correctly ?

    Thread Starter scottiescotsman

    (@scottiescotsman)

    no it lists all actors correctly but not the movies that they are in are all 0 and should be at least 1 for all

    Thread Starter scottiescotsman

    (@scottiescotsman)

    sorted ??

    <?php
    				
    				foreach ( get_terms( array(
    											'taxonomy' => 'actor',
    										) ) as $cat) :
    										?>
    					<div class="col-md-4">
    						<?php $the_query = new WP_Query( array(
    							'post_type' => 'movies',
    							'tax_query' => array(
    								array(
    									'taxonomy' => 'actor',
    									'field' => 'term_id',
    									'terms' => $cat->term_id,
    								)
    							)
    						) );
    						$count = $the_query->found_posts; ?>
    						
    						<?php echo $cat->name; ?> <span style="color: white"><?php echo $count; ?></span>
    						
    					</div>
    				
    				<?php endforeach; ?>

    That’s great !!!

    anurag.deshmukh

    (@anuragdeshmukh)

    I guess now you can change the topic status as ‘Resolved’ !!!

    Thread Starter scottiescotsman

    (@scottiescotsman)

    resolved

    Moderator bcworkz

    (@bcworkz)

    Hi @scottiescotsman,

    You asked in a related topic (closed and referred here) why this code doesn’t work:

    <?php $count_actor = get_term( 'actor' ); ?>
    
    <?php echo $count_actor->count; ?>

    It’s because ‘actor’ is an invalid parameter for get_term(). You must pass the term ID. If the term ID is not available for some reason, use get_term_by() to get a term object by its name or slug field.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘count custom taxonomy’ is closed to new replies.