• Resolved dilbert16588

    (@dilbert16588)


    Hi,

    I’m trying to find a nice easy way to estimate the word count automatically for my site without either manually figuring it out for each and every page – or writing my own code within my child theme. I THINK your plugin can help… but a running into similar issue others have had: that the plugin at default only includes ‘post_content’ and ignores everything else.

    I see in two other forum posts (https://www.ads-software.com/support/topic/custom-fields-290/ & https://www.ads-software.com/support/topic/adding-to-count-with-acf-flexible-content/) that you’ve added the ‘rtwp_filter_wordcount’ filter along with the ‘up_the_count’ function. While I can follow the gist of both answers, it seems like they understood coding a bit better than my piece-meal dabbling.

    My setup is a child of the Hello Elementor theme + Elementor Pro to build my page templates.
    – First of all, I see that the plugin already includes my CPT ‘review’ = perfect!
    – I KNOW I can use the above filter to include other ACF fields in the total count for the post/page/review.
    – But while the template for ‘review’ has two fields: post_content and another WYSIWYG ACF field called ‘queerrelevance’, my template for “Top 5” posts includes the original post_content plus another 5 WYSIWYG ACF fields. Another template only uses the ‘post_content’ plus 3 additional WYSIWYG ACF fields.

    If I understand the filter correctly, if I lump all the various WYSIWYG ACF fields into one filter and that page doesn’t have all of those fields, it’ll start kicking back errors? But equally if I start adding in conditional functions along with the filters, I feel like it’ll get boggled down with excess coding and defeat the simplicity of using the plugin. Is there something I am missing?

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Jason Yingling

    (@yingling017)

    It shouldn’t kick back errors. If you use get_field() I believe it will just return false if the field doesn’t exist for a particular post. So you should be able to do it in a single filter but you will want to only add the number of words from that field to the total if get_field returns something other than false.

    Thread Starter dilbert16588

    (@dilbert16588)

    Hi,

    Gave it a try… but it’s still not including the other ACF fields in the total count.

    I’m using the following post in question as reference: https://queerfilmreviews.com/reviews/thelaramieproject/
    The reading time underneath the page title is manually calculated without the plugin and what I’m trying to match = 19 min read. At the very bottom of the main content block (just above “My Rating Breakdown” is the result from the plugin with the filters added = 13 min read (which is just the main post_content block).

    I tried two different methods:

    add_filter( 'rtwp_filter_wordcount', 'up_the_count' );
    function up_the_count( $count ) {
    return $count += $ACFcount;
    
    $ACFcount=array(
        preg_split( '/\s+/', get_field( 'imdbsynopsis', ) ),
        preg_split( '/\s+/', get_field( 'queerrelevance', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_movie_1', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_movie_2', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_movie_3', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_movie_4', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_movie_5', ) ),
        preg_split( '/\s+/', get_field( 'pscontentbox', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_season_1', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_season_2', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_season_3', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_season_4', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_season_5', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_season_6', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_season_7', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_season_8', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_season_9', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_season_10', ) ),
        preg_split( '/\s+/', get_field( 'synopsis_season_11', ) ),
    );
    }

    I then tried this route:

    add_filter( 'rtwp_filter_wordcount', 'up_the_count' );
    function up_the_count( $count ) {
    	return $count += count(preg_split( '/\s+/', get_field( $ACFcount, ) ),);
    
    $ACFcount=array('imdbsynopsis', 'queerrelevance', 'synopsis_movie_1', 'synopsis_movie_2', 'synopsis_movie_3', 'synopsis_movie_4', 'synopsis_movie_5', 'pscontentbox', 'synopsis_season_1', 'synopsis_season_2', 'synopsis_season_3', 'synopsis_season_4', 'synopsis_season_5', 'synopsis_season_6', 'synopsis_season_7', 'synopsis_season_8', 'synopsis_season_9', 'synopsis_season_10', 'synopsis_season_11', );
    }

    Again, I’m certainly no expert and had to reference the get_field() format while tweaking what worked for the other two users. I probably have something wrong – just not sure what that might be. Thoughts?

    • This reply was modified 4 years, 1 month ago by dilbert16588.
    • This reply was modified 4 years, 1 month ago by dilbert16588.
    Plugin Author Jason Yingling

    (@yingling017)

    Your return statement should be the last part of your function. That takes you out of the function and returns the value.

    add_filter( 'rtwp_filter_wordcount', 'up_the_count' );
    function up_the_count( $count ) {
    
    	$ACFcount=array(
    		preg_split( '/\s+/', get_field( 'imdbsynopsis', ) ),
    		preg_split( '/\s+/', get_field( 'queerrelevance', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_movie_1', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_movie_2', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_movie_3', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_movie_4', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_movie_5', ) ),
    		preg_split( '/\s+/', get_field( 'pscontentbox', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_1', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_2', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_3', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_4', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_5', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_6', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_7', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_8', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_9', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_10', ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_11', ) ),
    	);
    	
    	return $count += $ACFcount;
    	
    }

    The return statement will end your function and return the value at that point.

    If that’s still not getting you the desired result you may need to get the post ID and pass it to the get_field() function. One way to do that is to use get_the_ID().

    add_filter( 'rtwp_filter_wordcount', 'up_the_count' );
    function up_the_count( $count ) {
    
    	$post_id = get_the_ID();
    
    	$ACFcount=array(
    		preg_split( '/\s+/', get_field( 'imdbsynopsis', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'queerrelevance', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_movie_1', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_movie_2', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_movie_3', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_movie_4', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_movie_5', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'pscontentbox', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_1', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_2', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_3', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_4', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_5', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_6', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_7', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_8', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_9', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_10', $post_id ) ),
    		preg_split( '/\s+/', get_field( 'synopsis_season_11', $post_id ) ),
    	);
    	
    	return $count += $ACFcount;
    	
    }
    Thread Starter dilbert16588

    (@dilbert16588)

    Oops, Thought that as long as it was wrapped in the function itself it wasn’t critical.

    Running into a sorta different issue now when I put the return at the end, I get a critical error and it references back to the return $count += $ACFcount; line in my functions. Not sure why this is creating issue though because it should work.

    Or a thought – is $ACFcount used somewhere else in my theme and how I have incorporated ACF? Thus, because it’s being used twice, its creating the break.

    Thread Starter dilbert16588

    (@dilbert16588)

    Well, changing the $ACFcount name didn’t work (unless I somehow picked another one already in use!)

    Here’s the full error code it kicked back. Definitely starting to get lost in things… but again, it seems like the code should work.

    Fatal error: Uncaught Error: Unsupported operand types in /home/customer/www/queerfilmreviews.com/public_html/wp-content/themes/hello-theme-child-master/functions.php:112 Stack trace: #0 /home/customer/www/queerfilmreviews.com/public_html/wp-includes/class-wp-hook.php(287): up_the_count(3305.6666666667) #1 /home/customer/www/queerfilmreviews.com/public_html/wp-includes/plugin.php(206): WP_Hook->apply_filters(3305.6666666667, Array) #2 /home/customer/www/queerfilmreviews.com/public_html/wp-content/plugins/reading-time-wp/rt-reading-time.php(154): apply_filters(‘rtwp_filter_wor…’, 3305.6666666667) #3 /home/customer/www/queerfilmreviews.com/public_html/wp-content/plugins/reading-time-wp/rt-reading-time.php(244): Reading_Time_WP->rt_calculate_reading_time(2806, Array) #4 /home/customer/www/queerfilmreviews.com/public_html/wp-includes/shortcodes.php(343): Reading_Time_WP->rt_reading_time(Array, ”, ‘rt_reading_time’) #5 [internal function]: do_shortcode_tag(Array) #6 /home/customer/www/queerfilmreviews.com/public_html/wp-inc in /home/customer/www/queerfilmreviews.com/public_html/wp-content/themes/hello-theme-child-master/functions.php on line 112

    (line 112 was the ‘return $count += $ACFcount;’ line)

    Plugin Author Jason Yingling

    (@yingling017)

    Sorry I didn’t look at your function fully. You need to iterate over your $ACFcount array to get the numbers out of there and add them individually. Your Unsupported operand types error is from trying to add an array() to an integer.

    Probably best to store the count() in the array then loop over it to add it to the $count.

    add_filter('rtwp_filter_wordcount', 'up_the_count');
    function up_the_count($count) {
    
      $ACFcount = array(
        count( preg_split('/\s+/', get_field('imdbsynopsis',)) ),
        count( preg_split('/\s+/', get_field('queerrelevance',)) ),
        count( preg_split('/\s+/', get_field('synopsis_movie_1',)) ),
        count( preg_split('/\s+/', get_field('synopsis_movie_2',)) ),
        count( preg_split('/\s+/', get_field('synopsis_movie_3',)) ),
        count( preg_split('/\s+/', get_field('synopsis_movie_4',)) ),
        count( preg_split('/\s+/', get_field('synopsis_movie_5',)) ),
        count( preg_split('/\s+/', get_field('pscontentbox',)) ),
        count( preg_split('/\s+/', get_field('synopsis_season_1',)) ),
        count( preg_split('/\s+/', get_field('synopsis_season_2',)) ),
        count( preg_split('/\s+/', get_field('synopsis_season_3',)) ),
        count( preg_split('/\s+/', get_field('synopsis_season_4',)) ),
        count( preg_split('/\s+/', get_field('synopsis_season_5',)) ),
        count( preg_split('/\s+/', get_field('synopsis_season_6',)) ),
        count( preg_split('/\s+/', get_field('synopsis_season_7',)) ),
        count( preg_split('/\s+/', get_field('synopsis_season_8',)) ),
        count( preg_split('/\s+/', get_field('synopsis_season_9',)) ),
        count( preg_split('/\s+/', get_field('synopsis_season_10',)) ),
        count( preg_split('/\s+/', get_field('synopsis_season_11',)) ),
      );
    
      foreach($ACFcount as $words) {
        $count += $words;
      }
    
      return $count;
    
    }
    Thread Starter dilbert16588

    (@dilbert16588)

    That worked!

    Thought I was trying to pull out an interger value with the second option I tried – but looks like even that wasn’t quite right without the foreach() inclusion.

    Thanks for the help. Hopefully it can help anyone else later who use more than one post_content fields.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Include word count for ACF fields, CPT, and multiple templates’ is closed to new replies.