Example: I'm trying to modify an instantiation of WP_Query by using the posts_orderby filter like so:

add_filter('posts_orderby', 'favorites_orderby');
$new_query = new WP_Query($args);
remove_filter('posts_orderby', 'favorites_orderby');

the favorites_orderby() function is like so:

function favorites_orderby($post_in){
        $post_in_str = implode(',', $post_in);
    return $post_in_str;

$post_in is an array that changes based on a previous, separate db query, so I need to be able to pass it into the favorites_orderby() function. Basically it's an array of post IDs that need to get fed to the WP_Query instantiation's ORDER BY parameter so that the resulting db request is set like so: ORDER BY FIELD(ID, 6, 18, 90, 12).

Everything works except passing the array of post IDs ($post_in) to the favorites_orderby()

Any help would be greatly appreciated!


No, filters get arguments based on the call to the filter. Basically, filters get their inputs from the apply_filters function call.

If you want to pass in data via another means, use a global variable or wrap the code and the data you want to use in an instance of a class.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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