Opened 3 years ago
Closed 3 years ago
#12723 closed defect (bug) (fixed)
apply_filters_ref_array bug
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | highest omg bbq | Milestone: | 3.0 |
| Component: | Plugins | Version: | 3.0 |
| Severity: | blocker | Keywords: | has-patch tested commit |
| Cc: | jeff@… |
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)
- Milestone Unassigned deleted
- Resolution set to invalid
- Status changed from new to closed
- Milestone set to 3.0
- Resolution invalid deleted
- Status changed from closed to reopened
On a closer look, bug confirmed.
- Keywords dev-feedback removed
- Owner changed from westi to scribu
- Status changed from reopened to accepted
comment:5
jfarthing84 — 3 years ago
Huh, oops. I sure did. I didn'tforget in my code though, just on typing the example here.
comment:7
jfarthing84 — 3 years ago
Yes, it does.
- Owner changed from scribu to westi
- Status changed from accepted to assigned
comment:10
westi — 3 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
comment:11
jfarthing84 — 3 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.
comment:12
jfarthing84 — 3 years ago
- Milestone set to 3.0
comment:13
westi — 3 years ago
Ok I missed the subtlety of the bug and the need for multiple filters.
Sorry.
I've added a test case for this now to wordpress-tests so it won't reappear in the future.
comment:14
westi — 3 years ago
- Resolution set to fixed
- Status changed from reopened to closed

Erm, you forgot to do return $where in both cases.