AJAX filter posts on click based on category

This line in functions.php is your problem:

$cat_id = get_post_meta($_REQUEST['cat']);

I think you are misunderstanding the purpose of the get_post_meta() function. It is designed to get metadata for a WordPress Post, not data from a POST request to the site. The first parameter of the get_post_meta() function is the $post_id, but since you are passing the category ID instead, you’ll either get false or an array of all meta values if a post with the same ID as that category ID exists. In either of those cases, the value of the cat query var will not be a valid categroy ID, and so that part of the query will be ignored and all posts will be returned. If you change that line to this, it will fix that problem and your code will probably work:

$cat_id = absint( $_REQUEST['cat'] );

The absint() function just converts the user-supplied value into a positive integer.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)