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:

Read More

$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

Related posts

Leave a Reply

3 comments

  1. 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.

  2. In a plugin or in your theme’s functions.php just write:

    add_filter( 'getarchives_where', 'wpse_67264_filter_getarchives_where' );
    
    /**
     * Add post type 'events' to wp_get_archives() query.
     * 
     * No parameters needed because we ignore them anyway.
     * 
     * @wp-hook getarchives_where
     * @return  string New WHERE clause
     */
    function wpse_67264_filter_getarchives_where()
    {
        return "WHERE post_type = 'post' OR post_type = 'events' AND post_status = 'publish'";
    }
    

    That is better than a custom WP_Queryin my opinion, and you don’t even need to accept any parameters in your callback because you don’t use theme. 🙂

    To understand how filters work read the Codex on the Plugin API. Basically, your callback return value will replace the second part in apply_filters().

  3. What you’ll actually want to do is use the built-in WordPress class called WP_Query. This way you won’t be changing the actual WordPress core functionality (which would give yourself major headaches when it’s time to upgrade).

    In your archives page, replace your current loop with a custom query like so:

    $args = array(); // setup the custom posts you want in here
    $custom_query = new WP_Query($args); // build a custom query
    while($custom_query->have_posts()) : $custom_query->the_post();
        // inside the loop, show your posts
    endwhile;
    wp_reset_postdata(); // reset the query
    

    The $args array is simply your custom logic of which posts should show up inside this loop. See the WP_Query documentation for a full list of things you can put into this array.

    For example, you want to filter on post type, so see this section of the document and you will notice that you can build your $args array like so:

    $args = array('post_type' => array('post', 'events'))
    

    Hope that helps!

    [edit]

    I just realized that you might be wanting to obtain a simple unordered list of posts in the archive (and not necessarily display the posts themselves, since that’s actually how wp_get_archives behaves).

    Simply build your own unordered list by wrapping the loop in a <ul> tag and then inside the loop output <li> tags around the title of the archived post and the permalink to the archived post.