Support » Plugins » Hacks » Pass variable to filter?

  • Writing a function to do a wp_query with a ‘where’ clause:

    function nc_custom_query($my_where_clause){
    function nc_dynamic_where( $where = '' ) {
      $where .= $my_where_clause;
      return $where;
    add_filter( 'posts_where', 'nc_dynamic_where' );
      $dynamic = new WP_Query( array( 'post_type' => 'my_type', 'orderby' => 'rand', 'posts_per_page' => $number ));
    remove_filter( 'posts_where', 'nc_dynamic_where' );
    return $dynamic;

    Only this does not work…. since the filter funtion nc_dynamic_where can only accept $where and ouput $where

    So, how do I get the variable $my_where_clause into the filter?

    Caveat: I’m trying to avoid global

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz


    You can use a closure if using PHP 5.3 or later. If you are distributing your code to others, you may want to avoid this technique for a while as there are still older versions out and about.

    You could also use a class object to store data, but IMO, it’s just a glorified OOP type of global. But if done properly, it does avoid some of the ugliness brought on by globals.

    More Info.

    Thanks for the link. I’ll play around with those techniques. (Though to be honest, I just threw up my hands and went “global” with that bad boy… )

    Edit: Though for closure / anonymous functions – I was under the impression they can’t be unregistered as filters…

    Moderator bcworkz


    Though for closure / anonymous functions – I was under the impression they can’t be unregistered as filters…

    Hmm. Haven’t heard that one yet, not like I have my finger on the pulse of hard core development though, so a rather meaningless comment. Off the top of my head, I don’t see why not based on what little I know. I’m curious enough to want to setup a little test, but unfortunately I don’t currently have access to my test installation. So unless someone else chimes in, it’ll have to remain a mystery for a while.

    I could be wront, but it looks like a function gets registered with a unique ID: an spl_object_cache of the function in question (see: _wp_filter_build_unique_id() ) . So, in order to de-register an anonymous function, one would need to ensure the remove_filter function call is the exact same…

    Not sure myself – but I’ve just seen it tossed out a number of times that WP can’t un-register anonymous functions.

    Anyway, I’ll just play it “safe” and pollute the place with globals for the time being.

    Moderator bcworkz


    Ah, I see. I’m sure you are right. I just mistook your meaning. I might have said de-registering such a filter is very awkward or difficult rather than you simply can’t. End result is the same, I got lost in silly semantics. End result is don’t do it that way if you intend on (or someone else may want to) de-registering, it’s not worth the trouble. Thanks for clarifying.

    I don’t think globals are such a crime in any case. Yes, they have issues, and we must avoid using them if there’s a reasonable alternate. But creating tricky artifices just so we can pat ourselves on the back that we do not use globals is a bit of a self deception IMO. WP uses globals all over the place. A few more used very thoughtfully is not going to transform the installation into a complete mess. (at least no more than it already is 😉 )

    I’ve no doubt offended many coders who consider their code artful, in which shortcuts such as globals are as horrid as a garish inappropriate brushstroke across a painting. I understand, as well as appreciate, and even applaud such an approach. But they need to understand not everyone is talented enough, or even sees the need for such an approach. As long as my code does what it’s supposed to with reasonable efficiency, is well organized, runs cleanly, and is easy to follow and maintain, I feel I’ve done my job, with or without globals.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Pass variable to filter?’ is closed to new replies.