• Hi!

    I am a newbie on wordpress and php and I have my mind blowing because I am trying to find a way to compile this code that I found here

    <?php
    global $wp_query;
    $curauth = $wp_query->get_queried_object();
    $post_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'post' AND post_status = 'publish'");
    ?>
    <h2>Post Count: <?php echo $post_count; ?></h2>

    My goal is to show the count of published posts foreach custom post_type in author.php and I found a way to do it but this is not the right way. You can see my code here http://pastebin.com/6cSxs4th.

    How can I compile this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • PHP doesn’t compile 🙂

    You only need this part once:

    global $wp_query;
    $curauth = $wp_query->get_queried_object();

    And you don’t really need to be counting ‘all’ (*), just one column though I am not sure if that effects performance.

    Otherwise, what doesn’t work? The sql looks right but you can get into trouble directly querying the database like that. You are usually better off running get_posts or making a new WP_Query object.

    Thread Starter rolf deck

    (@rolf-deck)

    Hi s_ha_dum!

    This is my current code to output post counting by $curauth in author.php that I found here.

    <?php $post_author = get_query_var('author');
    $numpost = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $post_author AND post_type IN ('post') and post_status = 'publish'" );
    echo $numpost ?>

    This way I have to repeat this code for every custom post_type to retrieve the post counting for that particular author… So, my question is:

    Is there a way to do the query once and define all the variables (I don’t know if this is the term to apply…) to each post_type in order to have an “echo” for each counting?

    eg.
    echo $listing
    echo $post
    etc?

    Or do I have to repeat the query for each post_type? My concerns goes all to the performance issue that you mentioned above…

    You also say that

    The sql looks right but you can get into trouble directly querying the database like that. You are usually better off running get_posts or making a new WP_Query object.

    and I appreciate that you explain me why, please.

    I put this query inside an include that’s being pulled to the author.php (

    <?php include (TEMPLATEPATH . ‘/includes/es-author.php’); ?>

    . Is this a security breach?

    Is there a way to do the query once and define all the variables (I don’t know if this is the term to apply…) to each post_type in order to have an “echo” for each counting?

    Yes. You can query once for all of the information but then you need to process the information to split up by post type.

    You would need subqueries like

    SELECT (SELECT COUNT(ID) FROM $wpdb->posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'listing' AND post_status = 'publish'") as listing_count, SELECT (SELECT COUNT(ID) FROM $wpdb->posts WHERE post_author = '" . $curauth->ID . "' AND post_type = 'listing' AND post_status = 'publish'") as post_count, ...

    Subqueries should perform just fine in this case.

    …and I appreciate that you explain me why, please.

    WordPress has an increasingly complicated database structure. Changes to that can break things but if you use the core functions you have a better chance of surviving an update without issue.

    include(...) isn’t a security problem unless you are including something with a user defined variable in it. I’d consider using get_template_part() though.

    Thread Starter rolf deck

    (@rolf-deck)

    Hi s_ha_dum!

    Can you please take a look at this http://pastebin.com/kyqcqENE and explain me where I’m failing to retrieve the post’s count by post_type from the $curauth?

    The only thing that this outputs is the name of the post’s type’s (postlistinganswerquestion)…

    By the way, this: <?php get_template_part("es-author.php"); ?> should be the right way to call the file inside of the template, correct?

    Thanks!

    You aren’t using subqueries, like I said. Look at my code again.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Count several custom post_types of an specific author in Author.php’ is closed to new replies.