2

I have this code, but it shows the total number of posts published by post type 'code'.

<?php
$count_posts = wp_count_posts('code')->publish;
echo $count_posts;
?>

I have 2 post types: 'code' and 'shop'.
Post type code is "a child" for post type 'shop'.
Therefore I need to show the post count for the "child" post type 'code' for those attached to the "parrent" post type 'shop'.

UPDATE - SOLUTION (if any others are facing same problem) :

<?php
                            $result = $wpdb->get_results(
                                $wpdb->prepare(
                                    "SELECT COUNT(*) AS code_num FROM {$wpdb->prefix}postmeta AS postmeta LEFT JOIN {$wpdb->prefix}postmeta AS postmeta1 ON postmeta.post_id = postmeta1.post_id WHERE postmeta1.meta_key = 'code_expire' AND postmeta1.meta_value > %d AND postmeta.meta_key = 'code_shop_id' AND postmeta.meta_value = %s",
                                    current_time('timestamp'),
                                    $post->ID
                                )
                            );
                            $result = array_shift( $result );
                            echo '
                                <li class="list-group-item">
                                    <span class="badge">'.$result->code_num.'</span>
                                    <a href="'.esc_url( get_permalink( $post->ID ) ).'"> '.$post->post_title.' </a>
                                </li>';
    ?>

Big thanks to Ahmed!

1

If you prefer the $wpdb direct queries you can use something like this:

global $wpdb;   
$count = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_type = 'code' and post_status = 'publish'" );
echo $count;

In the sql query above change the post_type to whatever you like. This will return count of published posts under specific post type only.

If you want to query counts from multiple post types do this query:

global $wpdb;   
$count = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_type IN ( 'code', 'shop', 'post', 'etc' ) and post_status = 'publish'" );
echo $count;

Hope that helps.

  • Thanks Ahmed, but i need to check for post type 'code' attached to post type 'shop' - It is 2 post types which are "attached" as parrent and child. A part of my theme uses this code to recieve the right count - but i cant get it to function in my other part of the theme files: "SELECT COUNT(*) AS code_num FROM {$wpdb->prefix}postmeta AS postmeta LEFT JOIN {$wpdb->prefix}postmeta AS postmeta1 ON postmeta.post_id = postmeta1.post_id WHERE postmeta1.meta_key = 'code_expire' AND postmeta1.meta_value > %d AND postmeta.meta_key = 'code_shop_id' AND postmeta.meta_value = %s", – Kasper Sep 19 '16 at 22:29
  • The second query you suggested takes my "code posts" and pluses it with my 'shop posts'. instead i need the following: number of posts with post type 'code' which are attached to current post type 'shop' or "current page/url" if that makes sense? :-) – Kasper Sep 19 '16 at 22:34
  • How are they attached? I mean is it a custom field which defines that this post is a child of that post type? If it's a custom field relationship, please give me it's key and the expected value. – Ahmed Fouad Sep 19 '16 at 22:37
  • Yes, like that. I am not sure what you mean by key and expected value. In the functioning query it uses: postmeta1.meta_key = 'code_expire' AND postmeta1.meta_value > %d AND postmeta.meta_key = 'code_shop_id' AND postmeta.meta_value = %s" – Kasper Sep 19 '16 at 22:44
  • Can you paste the full functioning query in your edited question? Please. – Ahmed Fouad Sep 19 '16 at 22:51
0
$count = 0; 
$loop = new WP_Query( array('post_type' => 'code') );

while ( $loop->have_posts() ) : $loop->the_post();
  $count = $count + 1;
endwhile;

echo $count;
  • Thank you Ryan, but this gives me the same result as the code i pasted. The problem is that post type 'code' shows the total count of posts. But i need the toal count for the "parrent" post type 'shop' for which the post type 'code' is posted in. The structure is like: Post type 'shop' = parrent "category", Post type 'shop' = child "category" - so my 'code' post type is attached to 'shop' post type :-) – Kasper Sep 19 '16 at 22:24
  • Change the post type from 'code' to 'shop' – RyanCameron.Me Sep 19 '16 at 22:27
  • 2
    Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Sep 19 '16 at 22:32
  • Sorry i am so bad at explaining. i need the count for post type 'code' like you suggested. But i need only count of 'code' for those that are attached to post type 'shop' (like 'shop' is a parrent category, and 'code' is the child element) Does it make sense? :-) – Kasper Sep 19 '16 at 22:39

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.