Overriding wp_get_archives() apply_filters()

I have Googled for a while and I am not sure how is the best way to do this.

In wp-includes/general-template.php I am looking at the function wp_get_archives() which has this line of code in:

$where = apply_filters( 'getarchives_where', "WHERE post_type="post" AND post_status="publish"", $r );

What I want to do is have:

$where = apply_filters( 'getarchives_where', "WHERE post_type="post" OR post_type="events" AND post_status="publish"", $r );

However I am not sure how to hook into this filter and override it in functions.php

Any advice appreciated.

Thanks,

Ian

3 Answers
3

A WordPress filter is a function that takes in a string, array, or object, does something to it, and returns that filtered string, array, or object.

So what you want to do is turn "WHERE post_type="post" AND post_status="publish"" into "WHERE post_type="post" OR post_type="events" AND post_status="publish"". That is fairly straightforward.

From the looks of things, the getarchives_where filter accepts two arguments. So you’ll hook onto the filter like so:

add_filter( 'getarchives_where', 'my_fancy_filter_function', 10, 2 );

Then you need to write a function that takes in two parameters, filters them, and returns a string:

function my_fancy_filter_function( $text, $r ) {
    return "WHERE post_type="post" OR post_type="events" AND post_status="publish"";
}

Now, this function will take in any input, but it will always return the string for the filter you specified. There are much more advanced ways to add query parameters, but this will accomplish exactly what your question is asking.

Leave a Comment