it appears i have a problem with understanding how add_action,add_filter and apply_filters work.
somewhat abbreviated, i have the following scenario:
in a template file i have this
while ( $the_query->have_posts() ) : $the_query->the_post();
/*get meta data for this post**/
$meta=get_post_meta(get_the_ID(), $post_type, true );
/***
at this stage - amongst other things - print_r($meta) returns
Array ( [prices] => Array ( [0] => 8 ) )
***/
/**add add_action hook to change $meta (if possible)**/
do_action('alter_loop',$meta);
/***
AFTER executing this action I would like to have print_r($meta) return
Array ( [prices] => Array ( [0] => 5 ) )
but it still returns
Array ( [prices] => Array ( [0] => 8 ) )
***/
/* more things*/
endwhile;
in a class i have the following:
function __construct() {
add_action('alter_loop', array( $this, 'alter_loop_meta'),10,1);
}
function alter_loop_meta($meta){
add_filter('some_identifier',array($this,'my_filter'),10,1);
$meta = apply_filters('some_identifier',$meta);
/***
at this stage print_r($meta) DOES actually return
Array ( [prices] => Array ( [0] => 8 ) )
***/
return $meta;
}
function my_filter( $meta ) {
$meta['prices'][0]='8';
return $meta;
}
if I use
global $meta;
in the template file it works (and i can save myself all this filtering as i can just set $meta['prices'][0]='8'; in alter_loop_meta).
however, i am wondering if there's a way to achieve this without any global variables. any hints / links etc much appreciated and - of course - happy to provide more code if necessary ...