Hooks not activating in certain cases
-
I’ve had two separate cases where a hook written in Code Snippets and run via AJAX was ignored, but the hook worked fine when placed in ‘functions.php’.
The first hook is for ‘pre_get_posts‘, and the other hook is for a similar function ‘alm_query_args_{id}‘ from the plugin AJAX Load More.
-
Can you post an example of the code you are using?
Here are the two hook functions, both serving the same purpose, which is to hide all
product
-type posts in certain categories./* Hide products in excluded categories in post queries */ function hide_excluded_products( $query ) { $parent_term_id = 106; if ( !$query->get('post_type') == 'product' || ( $query->get('tax_query') && $query->get('tax_query')[0]['terms'] == $parent_term_id ) ) return; $excluded_post_ids = new WP_Query( array( 'post_type' => 'product', 'fields' => 'ids', 'tax_query' => array( array( 'taxonomy' => 'product_category', 'field' => 'term_id', 'terms' => $parent_term_id, 'operator' => 'IN', ), ), ) ); $query->set( 'post__not_in', array_merge( $query->get('post__not_in') ?: array(), $excluded_post_ids->posts ) ); } add_action( 'pre_get_posts', 'hide_excluded_products' ); /* Hide products in excluded categories in AJAX Load More queries */ function alm_hide_excluded_products( $args, $id ) { $parent_term_id = 106; if ( !$args['tax_query'] || $args['tax_query'][0]['terms'] != $parent_term_id ) { $excluded_post_ids = new WP_Query( array( 'post_type' => 'product', 'fields' => 'ids', 'tax_query' => array( array( 'taxonomy' => 'product_category', 'field' => 'term_id', 'terms' => $parent_term_id, 'operator' => 'IN', ), ), ) ); $args['post__not_in'] = array_merge($args['post__not_in'] ?: array(), $excluded_post_ids->posts); } return $args; } add_filter( 'alm_query_args_products_list', 'alm_hide_excluded_products', 10, 2);
Honestly, the content of the functions is probably irrelevant as I’m pretty sure the functions aren’t being called at all.
I can also confirm that the first hook does work when using
get_posts()
without AJAX.First of all, I’d recommend rewriting your snippets to remove the named functions, just in case they’re causing a conflict.
hide_excluded_posts
isn’t very generic of a name and I’d want to remove the possibility that’s causing any issues:/* Hide products in excluded categories in post queries */ add_action( 'pre_get_posts', function ( $query ) { $parent_term_id = 106; if ( ! $query->get( 'post_type' ) == 'product' || ( $query->get( 'tax_query' ) && $query->get( 'tax_query' )[0]['terms'] == $parent_term_id ) ) { return; } $excluded_post_ids = new WP_Query( array( 'post_type' => 'product', 'fields' => 'ids', 'tax_query' => array( array( 'taxonomy' => 'product_category', 'field' => 'term_id', 'terms' => $parent_term_id, 'operator' => 'IN', ), ), ) ); $query->set( 'post__not_in', array_merge( $query->get( 'post__not_in' ) ?: array(), $excluded_post_ids->posts ) ); } ); /* Hide products in excluded categories in AJAX Load More queries */ add_filter( 'alm_query_args_products_list', function ( $args, $id ) { $parent_term_id = 106; if ( ! $args['tax_query'] || $args['tax_query'][0]['terms'] != $parent_term_id ) { $excluded_post_ids = new WP_Query( array( 'post_type' => 'product', 'fields' => 'ids', 'tax_query' => array( array( 'taxonomy' => 'product_category', 'field' => 'term_id', 'terms' => $parent_term_id, 'operator' => 'IN', ), ), ) ); $args['post__not_in'] = array_merge( $args['post__not_in'] ?: array(), $excluded_post_ids->posts ); } return $args; }, 10, 2 );
Secondly, I notice you have conditionals which limit the situations when these functions do anything at the top of both functions. Have you checked that they are both still true for the desired situations during an AJAX request? I’d recommend simplifying both down, removing the conditionals, and adding some flag that’ll let you know whether or not they’re actually running.
I’ve simplified the functions way down to the following.
It’s now at least doing something, so that’s a start. I’ll have to look into this further another day.
Thanks for your help!
/* Hide products in excluded categories in post queries */ add_action( 'pre_get_posts', function ( $query ) { $parent_term_id = 106; $query->set( 'cat', $parent_term_id ); } ); /* Hide products in excluded categories in AJAX Load More queries */ add_filter( 'alm_query_args_products_list', function ( $args, $id ) { $parent_term_id = 106; $args['cat'] = $parent_term_id; return $args; }, 10, 2 );
I had the same thing happened to me. I reverted back to the previous version of the plugin 3.1.1. Removed the code snippet and then added it again. Everything started to work as normal. So I updated the plugin back to the currect version 3.2.0 and tested it again. Everything works fine now…
The only thing I can suggest is remove your code snippets and then re-add them. That seemed to fix the problem.
-
This reply was modified 2 years, 8 months ago by
johnboy85. Reason: Typo
I figured it out.
I set the snippet to only run on the site front end, and this caused the hooks to stop working. The same thing happens in ‘functions.php’ when using
is_admin()
.Still not sure why it’s happening, but it’s not a plugin issue so I’ll mark this as resolved.
Okay, so it was an AJAX problem.
Basically, the AJAX requests were being treated as if on an admin page:
Yep, that’s right – as AJAX is served through
wp-admin/
, it’s going to only be accessible to admin or global snippets.I probably should have checked what scopes you were using, but either way, glad you have it sorted now.
-
This reply was modified 2 years, 8 months ago by
- The topic ‘Hooks not activating in certain cases’ is closed to new replies.