I have a class whose constructor is the following:
public function __construct( $content_type, $filter_fields, $meta_query = '', $taxonomy = '') {
if ( !empty ( $content_type ) ) {
add_action('wp_ajax_myfilter', array($this, 'filter_function'));
add_action('wp_ajax_nopriv_myfilter', array($this, 'filter_function));
...
I create two instances of the class as follows (filter_list is an array that contains the properties which are not relevant for this purpose)
global $filter_builder;
foreach ($filter_list as $filter ) {
$filter_builder[$filter['content_type']] = new FilterBuilder(
$filter[ 'content_type' ], $filter[ 'filter_fields' ], $filter['meta_query'], $filter['taxonomy']
);
}
The instances are created correctly and everything shows fine until the ajax call is made and the callback function, 'filter_function', is invoked. In theory, each instance has their own callback function when they were created, however, every time that ajax call is made, the function the first instance of the global array is called.
The global array has two instances and when I try and debug the callback, it seems like the same callback is invoked for both instances.
Even though the callbacks have the same name, it should not make a difference. I have created an anonymous function and this odd behaviour continues.
Is there something I am missing? The callback function depends on the content type when it is invoked, and it's always defaulting to the post type that is defined first in the array.
Here is the instance creation simplified in case it helps:
$filter_list = array(
array(
'content_type' => 'event',
'filter_fields' => array(
// Text to Display => Field Name
'Suburb' => 'city',
'Postal Code' => 'zip',
),
'meta_query' => '',
'taxonomy' => '',
),
array(
'content_type' => 'contact_center',
'filter_fields' => array(
// Text to Display => Field Name
'Suburb' => 'suburb',
'Postal Code' => 'postal_code',
),
'meta_query' => '',
'taxonomy' => '',
),
);
If you get this far, I thank you, and if you can help, I thank you even more! :-)
wp_ajax_myfilter
andwp_ajax_nopriv_myfilter
the same for both classes? Or is themyfilter
part different for each class?wp_ajax_myfilter
andwp_ajax_nopriv_myfilter
in both classes. The callback is ok, but if the action name is the same for both, the ajax call will only go to one of them. What happens if you name themwp_ajax_nopriv_myfilter1
andwp_ajax_nopriv_myfilter2
?