Your cart is currently empty!
Check whether page is restricted
—
by
Hello, I’m simply trying to show/hide something on my page based on whether there are any group restrictions assigned to it.
I’m using this example code, but it is not working. The $post_is is returning correctly, but when I do a var_dump of $group_ids I get 0, regardless of whether groups are assigned to the page or not.
Any idea what I might be doing wrong?
$post_id = get_the_ID();
$group_ids = Groups_Post_Access::get_read_group_ids( $post_id );
if ( count( $group_ids ) > 0 ) {
// $group_ids holds one or more IDs of the groups that restrict read access to the post
echo 'This page is resticted';
} else {
echo 'there are no restrictions on this page';
Posted in Groups
Comments
3 responses to “Check whether page is restricted”
That’s great Mike, you’re welcome!
Cheers,
George
Hi Mike,
Welcome back to our support forum. Your code snippet seems to be correct, but it also depends on how/where do you use the snippet? Have you checked if the returning post id actually belongs to a valid post? BTW, it would also be wise to check whether the post id exists or has a valid value.
Kind regards,
George
Thanks for your reply, George.
I did some further checking on the $post_id and discovered that it was returning the incorrect post id. So your hunch was correct.
I replaced get_the_ID() with get_queried_object_id()
That seems to have done the trick.
Thanks!