1

I have a custom post type that have many taxonomies. In the front end, a user can assign a 'category' (term_meta) value to the taxonomy - hardware or software.

I wanted to be able to count how many term_meta posts there were.

 $base_array        = array(
    'posts_per_page'    => -1,
    'fields'            => 'ids',
    'post_type'         => 'cpt',
    'post_status'       => array('publish'),
    'date_query'        => array(
            'before'    => 'next Saturday',
            'after'     => 'last Monday'
    )
);

$base = get_posts($base_array);

echo count($base);

This will give me the total count of posts in the week. But I want to count the posts that have the taxonomy with the term_meta 'hardware' or 'software'.

Is that possible?

2
$base_array        = array(
    'posts_per_page'    => -1,
    'fields'            => 'ids',
    'post_type'         => 'cpt',
    'post_status'       => array('publish'),
    'date_query'        => array(
            'before'    => 'next Saturday',
            'after'     => 'last Monday'
    ),
    'tax_query'        => array(
            'taxonomy'  => 'pas_here_taxonomy'
            'field'    => 'slug',
            'terms'    => array( 'hardware', 'software'),
'operator'      => 'IN'
    )
);

$base = get_posts($base_array);

echo count($base);
  • I understand that, and it would work if the taxonomy has the terms hardware and software, but the terms themselves have the meta of hardware and software. That is, in the table wp_termmeta there are meta_keys and meta_value for each term. And the collective meta for the week across all taxonomies is what I'm trying to count – Ahhhhhhhhhhhhhdfgbv Oct 28 '16 at 22:43
1

So, looking at some other code I have, I feel this would be the right way to go about it. I haven't tested it, but I feel it would work in the way I needed it too (no longer need to count the types).

$base_array = array(
    'posts_per_page'    => -1,
    'fields'            => 'ids',
    'post_type'         => 'cpt',
    'post_status'       => array('publish'),
    'date_query'        => array(
        'before'    => 'next Saturday',
        'after'     => 'last Monday'
    )
);

$base = get_posts($base_array);


foreach( $base as $post_id ) {
    $term_meta = get_term_meta( post_id, 'tax_term_type', true );

    $term_hardware += ($term_meta == 'term_meta_hardware') ? 1 : 0;
    $term_software += ($term_meta == 'term_meta_software') ? 1 : 0;
}

echo count( $term_hardware );
echo count( $term_software );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.