3

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

5

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.

1
  • I like this option better than my solution using WP_Query
    – Jared Cobb
    Commented Oct 5, 2012 at 15:36
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().

1
  • I like this option better than my solution using WP_Query
    – Jared Cobb
    Commented Oct 5, 2012 at 15:37
1

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.

1
  • This solution will accomplish what you're trying to do... But see toscho's or EAMann's method for what I think is a better approach. Of course, if you need to be able to display these posts in a loop from an archives page, this one will work. (But I don't think that was exactly what you were trying to do).
    – Jared Cobb
    Commented Oct 5, 2012 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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