Check whether page is restricted

Posted in

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';

3 Responses to Check whether page is restricted

  1. George September 23, 2020 at 4:22 pm #

    That’s great Mike, you’re welcome!

    Cheers,
    George

  2. George September 22, 2020 at 7:23 pm #

    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

    • Mike Eberly September 22, 2020 at 8:20 pm #

      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!

We use cookies to optimize your experience on our site and assume you're OK with that if you stay.
OK, hide this message.

Affiliates · Contact · Jobs · Terms & Conditions · Privacy Policy · Documentation · Downloads · Useful Plugins · My Account

Share