Count the number of matching post names in foreach loop

I would get the count of a variable number of repeated titles by using an indexed array. $theposts = get_posts( array( ‘post_type’ => ‘post_subscriber’, ‘post_status’ => ‘publish’, ‘numberposts’ => -1, ) ); // project titles counter, outside the loop $project_titles = array(); foreach( $theposts as $p ): $project_name = get_post_meta($p->ID, ‘project’, true); echo ‘<ol>’; echo … Read more

Display post count on archive page in reverse order

You can give a try to this within a loop <?php echo $wp_query->found_posts – $wp_query->current_post ; ?> $wp_query->found_posts gives the total number of posts found matching the current query parameters. So the if there are 20 posts, result for each post should look like this For 1st post it will display 20, i.e. 20-0=20 For … Read more

Count images in post then add class if just one

Using @pascalvgemert’s suggestion as a starting point, I’m using the following script: jQuery(document).ready(function() { var imgCount = jQuery(“.single-post #content-left p a”).children(“img”).length; if (imgCount == 1) { jQuery(“img”).addClass(“lone-image”); } }); Works like a charm.

Count words for all posts by all authors

You will need a function to get all the published posts and count the words. function wpse410818_count_published_words() { $posts = new WP_Query(array( ‘post_type’ => array( ‘post’ ), ‘post_status’ => ‘publish’, )); $count = 0; //Starting words count if ( $posts->have_posts() ) { while ( $posts->have_posts() ) { $posts->the_post(); //Add to the word count $count += … Read more

How do I count the number of pages a user has seen on my site, and force them to log in after a certain amount

Background Gating unauthenticated end-user’s ability to view site content based on the activity of such users is a difficult problem to solve reliably, owed to the design of web technologies as a whole. To the best of my knowledge, WordPress core provides no features which require any such functionality, and so there are no core … Read more