0

I am trying to rebuild my inline posts counting to a function. What would be the best way to do that? Here is my inline code:

$posts = get_posts('post_type=myposttype&customtaxonomy=mytax&posts_per_page=-1');    
$posts_count = count($posts);
echo $posts_count;

In my functions.php I tried this:

function get_my_posts_count($mytaxonomy) {
    $mytaxonomy = get_posts('post_type=myposttype&$customtaxonomy=$mytaxonomy&posts_per_page=-1');
    $mytaxonomy_posts_count = count($mytaxonomy);
    echo $mytaxonomy_posts_count;
}

After that in my template files I call the function like this

<?php get_my_posts_count('taxonomyname');?>
3
  • Im sorry my post is such a messy. I did my best not to look like that, but that code wants crazy formatting skills. – worldwildwebdev Oct 10 '14 at 21:00
  • 1
    The code formatting is easier than you think ;-) Just add 4 spaces in front of each code line with an extra new line above the whole code block. You can also use the {} button do achieve that. – birgire Oct 10 '14 at 21:23
  • I'll keep that in mind! Many thanks for the advice! – worldwildwebdev Oct 10 '14 at 21:30
2

You have a couple of problems with your code

  • customtaxonomy=$mytaxonomy is incorrect. First of all, there is no parameter called customtaxonomy in get_posts. Secondly, your syntaxing is wrong. If you make use of a varaible, your syntaxing should look like this

    'post_type=myposttype&customtaxonomy=' . $mytaxonomy . '&posts_per_page=-1'
    
  • You should make use of a proper tax_query to get your posts. You should also make use of get_terms to get all the terms of the selected taxonomy

Here is how your code should look like: (I have also made the post type a variable)

function my_posts_count($taxonomy, $post_type) {
    $terms = get_terms( $taxonomy, 'fields=ids' );

    $args = [
        'post_type' => $post_type,
        'nopaging' => true,
        'tax_query' => [
            [
                'taxonomy' => $taxonomy,
                'field' => 'term_id',
                'terms' => $terms
            ]
        ],
    ];

    $mytaxonomy = get_posts( $args );
    $mytaxonomy_posts_count = count($mytaxonomy);

    echo $mytaxonomy_posts_count;
}

Please note, I have used syntaxing that will not work pre PHP 5.4

You can then use the code as follow

my_posts_count('category', 'post');

EDIT

Rethinking of @birgire comments

I feel we should return the count since we use the get_ prefix, even though WordPress breaks it's own rule regarding this semi naming convention

I have removed the get_ prefix from your function name. In Wordpress, functions which have the get_ prefix returns its output, while functions without the get_ prefix echo its output

The correct way to use your function when using the get_ prefix is as follows

function get_my_posts_count($taxonomy, $post_type) {
    $terms = get_terms( $taxonomy, 'fields=ids' );

    $args = [
        'post_type' => $post_type,
        'nopaging' => true,
        'tax_query' => [
            [
                'taxonomy' => $taxonomy,
                'field' => 'term_id',
                'terms' => $terms
            ]
        ],
    ];

    $mytaxonomy = get_posts( $args );
    $mytaxonomy_posts_count = count($mytaxonomy);

    return $mytaxonomy_posts_count;
}

And in your template call it like this

echo get_my_posts_count('category', 'post');
5
  • Your explanation make more sense! Thanks a lot! That worked fine for me! – worldwildwebdev Oct 11 '14 at 10:42
  • Glad it worked out for you. Enjoy :-) – Pieter Goosen Oct 11 '14 at 10:50
  • 1
    Just a small note here: I feel we should return the count since we use the get_ prefix, even though WordPress breaks it's own rule regarding this semi naming convention ;-) – birgire Oct 11 '14 at 12:07
  • @IgreogyVokoramich on recommendation from birgire, I have updated the code. Just two small changes. :-) – Pieter Goosen Oct 12 '14 at 6:54
  • 2
    @birgire Thanks once again. That was actually very sharp, and I totally agree with you on that point. Have updated my answer to show both methods – Pieter Goosen Oct 12 '14 at 7:24
1

In your function, you have this line:

$mytaxonomy = get_posts('post_type=myposttype&$customtaxonomy=$mytaxonomy&posts_per_page=-1');

Try changing it to:

$mytaxonomy = get_posts('post_type=myposttype&customtaxonomy=$mytaxonomy&posts_per_page=-1');

$customtaxonomy is a variable. customtaxonomy is a parameter.

1
  • Actually I misspell it. Tried the other method: adding the arguments to array and works like a charm. I don't understand why this short sorting of arguments does not accept variables? – worldwildwebdev Oct 10 '14 at 21:50

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.