WordPress.org

Ready to get started?Download WordPress

Forums

Count with get_term_by() ONLY the published posts (7 posts)

  1. Rockound
    Member
    Posted 2 years ago #

    Hello,

    I need your help please.

    I try to get the number of included posts in different taxonomies. It works fine but at the moment the function returns me the full number of posts and I only wan't to display the PUBLISHED posts in this taxonomy.

    Here is the actual code:

    $terms = array();
    foreach(explode(',', $cat) as $term_slug) {
    $terms[] = get_term_by('slug', $term_slug, 'portfolio_category');
    }
    foreach($terms as $term) {
    $output .= '<a data-value="' . $term->slug . '" href="#">' . $term->name . ' (' . $term->count . ')</a>';
    }

    Can you guys help me with my problem?

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    I use a function like this to get the published post count: http://pastebin.com/UqkLhzRc

    $count = published_term_count($term->term_id, 'portfolio_category');
  3. Rockound
    Member
    Posted 2 years ago #

    Thanks for your answer, but I get a error message when I try to integrate it to my code. Where I have to integrate the function from the pastebin link, and where I have to integrate the $count = published_term_count($term->term_id, 'portfolio_category'); ?

  4. Rockound
    Member
    Posted 2 years ago #

    UPDATE: I don't get a error message anymore. I integrated the function and call it in the foreach-loop. But now I get always the value "0" and not the right number of published posts.

    Do you know this problem?

  5. keesiemeijer
    moderator
    Posted 2 years ago #

    The function goes in your theme's functions.php.

    Try it like this

    $terms = array();
    foreach(explode(',', $cat) as $term_slug) {
    $terms[] = get_term_by('slug', $term_slug, 'portfolio_category');
    }
    if(!empty($terms)){
      $output = '';
      foreach($terms as $term) {
        $count = published_term_count($term->term_id, 'portfolio_category');
        $output .= '<a data-value="' . $term->slug . '" href="#">' . $term->name . ' (' . $count . ')</a>';
      }
      echo $output;
    }

    as you see in the function only the posts from the post type "post" are counted.

    AND $wpdb->posts.post_type IN ('post')

    If you want them also for another post type you can change it.

    AND $wpdb->posts.post_type IN ('post','portfolio')
  6. Rockound
    Member
    Posted 2 years ago #

    I made it already on wednesday. Your function was absolutely ideal for me, the reason for the malfunction is described in your last post ;-)

    This was my previous code:
    AND $wpdb->posts.post_type IN ('post')

    This is the code now:
    AND $wpdb->posts.post_type IN ('portfolio')

    I just switched the post type and it works perfect!

    Thanks for your help!

  7. Paul Phillips
    Member
    Posted 2 years ago #

    This helped me quite a bit too although I needed something to match against 2 different custom taxonomies and terms at the same time.

    Here's the query if anyone else wants it.

    SELECT COUNT( ID )
    	FROM $wpdb->posts
    	INNER JOIN $wpdb->term_relationships ON
    		( $wpdb->posts.ID = $wpdb->term_relationships.object_id )
    	INNER JOIN $wpdb->term_relationships AS tt1 ON
    		( $wpdb->posts.ID = tt1.object_id )
    	WHERE (
    		$wpdb->term_relationships.term_taxonomy_id
    		IN (
    			SELECT term_taxonomy_id
    			FROM $wpdb->term_taxonomy
    			WHERE term_id = $term_id_1
    			AND taxonomy = $custom_tax_1
    		)
    		AND tt1.term_taxonomy_id
    		IN (
    			SELECT term_taxonomy_id
    			FROM $wpdb->term_taxonomy
    			WHERE term_id = $term_id_2
    			AND taxonomy = $custom_tax_2
    		)
    	)
    	AND $wpdb->posts.post_type = 'post'
    	AND ( $wpdb->posts.post_status = 'publish' )

Topic Closed

This topic has been closed to new replies.

About this Topic