Ajax post filter by category

Hi everybody,
Im trying to make an ajax post filter by category.
So im following this tutorial: WordPress Filter Posts with Ajax without page reload or plugin | Weichie

So my problem is this hook:

add_action(‘wp_ajax_filter_projects’, ‘filter_projects’);
add_action(‘wp_ajax_nopriv_filter_projects’, ‘filter_projects’);

function filter_projects() {
$catSlug = $_POST[‘category’];

$ajaxposts = new WP_Query([
  'post_type' => 'post',
  'posts_per_page' => -1,
  'category_name' => $catSlug,
  'orderby' => 'menu_order', 
  'order' => 'desc',
]);
$response = '';

if($ajaxposts->have_posts()) {
  while($ajaxposts->have_posts()) : $ajaxposts->the_post();
    $response .= 'Results';
  endwhile;
} else {
  $response = 'empty';
}

echo $response;
exit;

}

When i put the following code in /functions.php it works, but when i move it to app/setup.php it doesn’t work.

Any idea?
is there any example on how to use ajax to call posts with Sage 10 ?

Thanks

setup.php is namespaced, which means the functions in your callbacks also need to be namespaced—otherwise WordPress tries to call functions that don’t exist. The other app files (or maybe even setup) should have examples of how to do this.

Thank you @alwaysblank
I did this:

  add_action('wp_ajax_filter_projects', __NAMESPACE__ . '\\filter_projects' );
  add_action('wp_ajax_nopriv_filter_projects', __NAMESPACE__ . '\\filter_projects' ); 

But still not working

Fixed thanks you @alwaysblank
I had to add a backslash to WP_Query()

This topic was automatically closed after 42 days. New replies are no longer allowed.