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.
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.
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.