2

I'm using a custom WP_query to display a carousel with the most recent post on a page that doesn't have a main loop. Inside the WP_query loop, I'm using get_the_excerpt() to retrieve the automatically generated excerpt of those posts. The posts do not have a custom excerpt set, neither do they have a <!--more--> tag. However, those generated excerpts contain a trailing ellipsis (...) (not an actual ellipsis, but three dots) at the end.

According to all related posts and the codex, I should be able to modify or remove the trailing dots by adding a filter to the excerpt_more hook. I added this code to my functions.php:

add_filter('excerpt_more', 'change_excerpt_more', 1);
function change_excerpt_more($more){
  return '';
}

But it just does nothing. I tried various variants of this (returning a test string, having the function not accept an argument, changing the priority of the add_filter call), but the generated excerpts don't change at all.

Am I doing something wrong? Does the filter not work with a custom WP_query? How can I fix this?

Thanks!

2

The third field in add_filter() is priority. Default WP hook priority is 10. You can omit the 1 altogether, or still declare number of arguments accepting by function by passing it as 4th field. add_filter('excerpt_more', 'change_excerpt_more', 10, 1);

OR for default: add_filter('excerpt_more', 'change_excerpt_more');


re: questions in comments

still wondering why it wouldn't work without having passed a priority though

you were passing a priority

The issue you were having was that you were passing a priority, (1), and that it was possibly lower than some other things using that hook. So it may have been running fine, but just getting overwritten after it was run.

The add_filter and add_action methods have four parts:

  1. the filter or action, defined by apply_filters('name'..., and do_action('name'..., respectively.
  2. the callback function to be applied, your change_excerpt_more()
  3. the priority, an integer value that defaults to 10
  4. the number of arguments expected by the callback (made available by the do_action and apply_filter)

So in the example in your question, you have passed 1 as a priority, since it is in the third field. Assuming you meant to define the single of argument ($more), you would need to explicitly pass a priority or empty field (wordpress would use the default priority).

your code:

This:

add_filter('excerpt_more', 'change_excerpt_more', 1);

Is the same as:

add_filter('excerpt_more', 'change_excerpt_more', 1, 1);


to define the single argument

Like so:

add_filter('excerpt_more', 'change_excerpt_more', , 1);

That is the same as:

add_filter('excerpt_more', 'change_excerpt_more', 10, 1);

And since excerpt more only has one argument to pass, that is also the same as: add_filter('excerpt_more', 'change_excerpt_more');


On Priority

Priority becomes the numerical key in an array of filters/actions applied to the hook. Sorted by these numerical indexes, they are run in that order. Since it is just an (int) used as an index, the only limitation is PHP_INT_MAX. Wordpress defaults to 10, allowing for hooks to run before or after them easily. With nothing else using the hook, a default priority will run after any core usage since it was added to the hook after. To be sure you are running your hook after anything added at default, you can pass a higher priority.

But again, your issue was that your priority was too high (1) due to that value being in the third slot of the add_filter. So it was running, and then anything hooked at default (could be theme, other plugin, even core) was running after it.

To know all things being hooked with their priority, you can could print_r() $wp_filter passing it the name of the hook as the key of the array.

function filter_print() {
    global $wp_filter;
    print_r( $wp_filter['excerpt_more'] );
    die();
}
add_action( 'shutdown', 'filter_print' );

[code from Howdy_McGee on this WPSE answer]

  • Not sure how that is supposed to answer my question ^^ It actually led me to an answer though, turns out I needed to use a higher priority for the filter function (still wondering why it wouldn't work without having passed a priority though). Still, your post doesn't really answer my question. If you edit your answer to mention the importance of a high priority, I can accept it. – MoritzLost Jun 29 '17 at 13:42
  • You are passing a priority of one by the way you have written it. – hwl Jun 29 '17 at 13:44
  • found a minute to expand on my comment. Hope it helps to make sense. – hwl Jun 30 '17 at 12:19
  • @hwlyesterday 'you were passing a priority' As I said, I tried adding the filter without passing a priority, but that didn't work as well. Probably the theme has a filter in place with a higher (lower? higher int = lower priority I guess) priority than the default of 10. Printing out the wp_filter is a very good idea, I'm sure I'll need that soon enough. Anyway, thanks for taking the time to expand on your answer! – MoritzLost Jul 1 '17 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.