WordPress.org

Ready to get started?Download WordPress

Forums

Count several custom post_types of an specific author in Author.php (6 posts)

  1. rolf deck
    Member
    Posted 2 years ago #

    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?

  2. s_ha_dum
    Member
    Posted 2 years ago #

    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.

  3. rolf deck
    Member
    Posted 2 years ago #

    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?

  4. s_ha_dum
    Member
    Posted 2 years ago #

    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.

  5. rolf deck
    Member
    Posted 2 years ago #

    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!

  6. s_ha_dum
    Member
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic