Title: Custom Excerpts causing WP_Post variable error
Last modified: June 9, 2019

---

# Custom Excerpts causing WP_Post variable error

 *  [Chris Lowry](https://wordpress.org/support/users/medical-revision/)
 * (@medical-revision)
 * [7 years ago](https://wordpress.org/support/topic/custom-excerpts-causing-wp_post-variable-error/)
 * I’ve been attempting to make a function that allows me to adjust excerpts. I’m
   fairly amateur at all this, so I’m not completely clear on exactly how add_filter
   actually works.
 * 1. Does it replace a filter with a new one, but requires the new one will pass
   the same arguments? For example, in my link it decides that $length is a WP_Object
   for some reason and then doesn’t work.
 * 2. Does this mean that I’ll always have to call my custom function to use the
   extra arguments? So, for example, rather than calling get_the_excerpt(”,5) to
   get a 5 word excerpt, I’ll need to call excerptional(”,5) to do it?
 *     ```
       function excerptional ($text,$length) {
       $raw_excerpt = $text;
       if ( '' == $text ) {
   
           $text = get_the_content('');
           $text = strip_shortcodes( $text );
   
           $text = apply_filters('the_content', $text);
           $text = str_replace(']]>', ']]>', $text);
           //delete all header text, gives you rubbish short paragraphs in excerpts otherwise
       	$regex = array ('#\<h1[^]]*\>(.*?)\</h1\>#','#\<h2[^]]*\>(.*?)\</h2\>#','#\<h3[^]]*\>(.*?)\</h3\>#','#\<h4[^]]*\>(.*?)\</h4\>#','#\<h5[^]]*\>(.*?)\</h5\>#','#\<h6[^]]*\>(.*?)\</h6\>#');
       	$text = preg_replace($regex, '$1', $text);
   
       	// if ($length=='') {$length = 'monkey';}
   
       	// change headers to paragraphs
       	// $text = str_replace(array('<h1>', '<h2>', '<h3>', '<h4>', '<h5>', '<h6>'),'<p>', $text);
       	// $text = str_replace(array('</h1>', '</h2>', '</h3>', '</h4>', '</h5>', '</h6>'),'</p>', $text);
   
           $allowed_tags = '<p>,<a>,<em>,<strong>';
           $text = strip_tags($text, $allowed_tags);
   
       	if ($length!='') {$excerpt_word_count = $length;}
       	else {$excerpt_word_count = 250;}
   
           $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); 
   
           $excerpt_end = '';
           $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
   
           $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
           if ( count($words) > $excerpt_length ) {
               array_pop($words);
               $text = implode(' ', $words);
               $text = $text;
           } else {
              $text = implode(' ', $words);
           }
   
           //explode to get the first two paragraphs
           $paras = explode("</p>",$text);
           //$text = "<strong>".$paras[0]."</strong>".$paras[1];
           $text = $paras[0];
       }
       return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
       }
       remove_filter('get_the_excerpt', 'wp_trim_excerpt');
       add_filter('get_the_excerpt', 'excerptional',10,2);
       ```
   
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcustom-excerpts-causing-wp_post-variable-error%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years ago](https://wordpress.org/support/topic/custom-excerpts-causing-wp_post-variable-error/#post-11620628)
 * WordPress keeps a list of actions and a list of filters. You add your function
   to the list by calling `add_filter`. The first parameter is indicates the filter
   name that the second parameter will be called for.
    [https://developer.wordpress.org/reference/functions/add_filter/](https://developer.wordpress.org/reference/functions/add_filter/)
 * The `apply_filters` function will call all the filters in the list for that filter
   name, in the order of the priority parameter.
    [https://developer.wordpress.org/reference/functions/apply_filters/](https://developer.wordpress.org/reference/functions/apply_filters/)
 * The filter only has access to the data that is passed in, and can only affect
   the first parameter (which must be returned).
    [https://developer.wordpress.org/plugins/hooks/filters/](https://developer.wordpress.org/plugins/hooks/filters/)
   [https://codex.wordpress.org/Plugin_API/Filter_Reference](https://codex.wordpress.org/Plugin_API/Filter_Reference)
 * Look here to see where the filter is called, and if you really need to do all
   of your code (or is it already being done?).
    [https://developer.wordpress.org/reference/functions/get_the_excerpt/](https://developer.wordpress.org/reference/functions/get_the_excerpt/)

Viewing 1 replies (of 1 total)

The topic ‘Custom Excerpts causing WP_Post variable error’ is closed to new replies.

## Tags

 * [add_filter](https://wordpress.org/support/topic-tag/add_filter/)
 * [excerpts](https://wordpress.org/support/topic-tag/excerpts/)
 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [variables](https://wordpress.org/support/topic-tag/variables/)
 * [wp_post](https://wordpress.org/support/topic-tag/wp_post/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Joy](https://wordpress.org/support/users/joyously/)
 * Last activity: [7 years ago](https://wordpress.org/support/topic/custom-excerpts-causing-wp_post-variable-error/#post-11620628)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
