Count all comments of a custom post type

How about direct sql with a simple subquery to first get all the post ids of your custom post type ex: function get_all_comments_of_post_type($post_type){ global $wpdb; $cc = $wpdb->get_var(“SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID in ( SELECT ID FROM $wpdb->posts WHERE post_type=”$post_type” AND post_status=”publish”) AND comment_approved = ‘1’ “); return $cc; } Usage: $total_comments = get_all_comments_of_post_type(‘some_post_type’);

Different permalink for CPT and regular Posts/Pages but why?

The permalink configuration in WordPress backend is only for standard posts and pages. For CPT the default URL structure is http://example.com/cpt-identifier/post-slug. If you want a different URL structure for you CPT you will have to define and register your own rewrite rules. For example: add_action( ‘init’, ‘register_posttype’ ); function register_posttype() { register_post_type( ‘my_cpt’, //this will … Read more

Loading all files within a directory

Loading files from a directory – the historical way The glob() function has three possible return values: Array( filenames ) => success Array() empty => nothing found FALSE => Error So the correct check would be $files = glob( sprintf( ‘%s/postypes/*.php’, get_stylesheet_directory() ) ); if ( empty( $files ) { _e( ‘No PHP files found’, … Read more

Query that joins the postmeta table twice

“My SQL doesn’t work” questions are difficult because they are nearly untestable without having the OP’s dataset, but… I think you may be making this too complicated. SELECT DISTINCT (p.ID), p.post_title FROM wp_posts p JOIN wp_postmeta pm1 ON p.ID = pm1.post_id JOIN wp_postmeta pm2 ON p.ID = pm2.post_id WHERE 1 = 1 AND p.post_type=”game” AND … Read more

Can I make WordPress use a custom template for a child page

You can filter template_include and replace the single-community.php with a single-child-community.php. Example add_filter( ‘template_include’, function( $template ) { if ( ! is_singular() ) return $template; // not single if ( ‘communities’ !== get_post_type() ) return $template; // wrong post type if ( 0 === get_post()->post_parent ) return $template; // not a child return locate_template( ‘single-child-community.php’ … Read more