Opened 14 years ago
Closed 14 years ago
#12723 closed defect (bug) (fixed)
apply_filters_ref_array bug
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 3.0 | Priority: | highest omg bbq |
Severity: | blocker | Version: | 3.0 |
Component: | Plugins | Keywords: | has-patch tested commit |
Focuses: | Cc: |
Description
I believe (unless I am not using it correctly) that there is a bug with the new apply_filters_ref_array() function. Particularly, I am applying two different functions certain 'posts_' filters in the query, and only the one with the highest priority is processed. Small example:
function posts_where_filter_one($where) { global $wpdb; $where .= " AND $wpdb->posts.post_type = 'post'"; } add_filter('posts_where', 'posts_where_filter_one'); function posts_where_filter_two($where) { global $wpdb; $where .= " AND $wpdb->posts.post_status = 'publish'"; } add_filter('posts_where', 'posts_where_filter_two');
You would expect $where to end up as:
AND wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish'
But instead it end up as:
AND wp_posts.post_status = 'publish'
Attachments (1)
Change History (15)
#1
@
14 years ago
- Milestone Unassigned deleted
- Resolution set to invalid
- Status changed from new to closed
#2
@
14 years ago
- Milestone set to 3.0
- Resolution invalid deleted
- Status changed from closed to reopened
On a closer look, bug confirmed.
#3
@
14 years ago
- Keywords dev-feedback removed
- Owner changed from westi to scribu
- Status changed from reopened to accepted
#5
@
14 years ago
Huh, oops. I sure did. I didn'tforget in my code though, just on typing the example here.
#10
@
14 years ago
- Milestone 3.0 deleted
- Resolution set to invalid
- Status changed from assigned to closed
Your filters are not written correctly.
A filter hooked into a WordPress filter must return something to be passed back out.
By default the first argument passed to apply_filters / apply_filters_ref_array is returned unless a function is hooked in.
If something is hooked in then the result of that function is returned instead.
If there are a number of filters then the result of one gets passed to the next as an argument.
Your examples should be:
function posts_where_filter_one($where) { global $wpdb; $where .= " AND $wpdb->posts.post_type = 'post'"; return $where; } add_filter('posts_where', 'posts_where_filter_one'); function posts_where_filter_two($where) { global $wpdb; $where .= " AND $wpdb->posts.post_status = 'publish'"; return $where; } add_filter('posts_where', 'posts_where_filter_two');
Closing as invalid
#11
@
14 years ago
- Resolution invalid deleted
- Status changed from closed to reopened
Wow, if you look up I said that I just forgot to type the return in my mockup examples here, not in my code. It is a bug. Try it yourself.
Erm, you forgot to do
return $where
in both cases.