There’s a couple issues here:
is_singular()
will return false if the function is called before the WP_Query object is set upuser_has_cap
hook fires early and often – and before WP_Query is set
We know that the WP_Query object is set at the wp
hook, so you can use that action hook to add a filter for user_has_cap
.
add_action( 'wp', 'sqms_wp_action' );
function sqms_wp_action() {
//* Since the WP_Query object is set, is_singular() should work properly
if ( ! is_singular( 'sqms_payne_dealer' ) ) {
return;
}
add_filter( 'user_has_cap', 'sqms_user_has_cap_filter' );
}
function sqms_user_has_cap_filter() {
//* No need for is_singular() check
//* Rest of code unchanged
}
So, most of your code was correct. It was just executed in the wrong order.