Support » Plugin: Groups » pre_get_posts exclusion of groups rule has stopped working

  • Resolved Thomas Clausen

    (@tntc1978)


    This function used to ensure, that titles of group “hidden” post were still shown on archives and front page here: aktieraadet.dk to tease the reader. And then when they clicked the link, they were forced to log in, or buy the access rights. Now only open (not group restricted) post are shown. I haven’t noticed when this happened. But got a complaint about it a couple of weeks ago.

    // Show title, excerpt and thumbnail in archives
    add_action('pre_get_posts', 'my_pre_get_posts');
    function my_pre_get_posts() {
    	// If the user is looking at the front page
    	if ( is_archive() || is_category() || is_home() || is_front_page() ) {
    		// Remove the Groups_Post_Access filters
    		remove_filter( 'posts_where', array( 'Groups_Post_Access', 'posts_where' ), 10, 2 );
    		remove_filter( 'the_posts', array( 'Groups_Post_Access', 'the_posts' ), 1, 2 );
    		remove_filter( 'get_the_excerpt', array( 'Groups_Post_Access', 'get_the_excerpt' ), 1 );
    		remove_filter( 'the_content', array( 'Groups_Post_Access', 'the_content' ), 1 );
    	}
    }

    I did a writeup about it a couple of years ago here: https://woocommerce.com/2015/03/customer-story-paid-newsletter-subscriptions-with-woocommerce/ back then the snippet worked like a charm. Any thoughts on what happened, or what could fix it would be greatly appreciated.

    • This topic was modified 3 years, 7 months ago by Thomas Clausen. Reason: Elaborating on the term hidden
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Kento

    (@proaktion)

    Hi Thomas,

    I’ve just tested your code with a Canvas child theme with the code added to its functions.php – from Groups 2.x on you will need to include cases for legacy mode into account (which I assume you have activated on your site). So the updated code would look like this:

    // Show title, excerpt and thumbnail in archives
    add_action('pre_get_posts', 'my_pre_get_posts');
    function my_pre_get_posts() {
    	// If the user is looking at the front page
    	if ( is_archive() || is_category() || is_home() || is_front_page() ) {
    		// Remove the Groups_Post_Access filters
    		remove_filter( 'posts_where', array( 'Groups_Post_Access', 'posts_where' ), 10, 2 );
    		remove_filter( 'the_posts', array( 'Groups_Post_Access', 'the_posts' ), 1, 2 );
    		remove_filter( 'get_the_excerpt', array( 'Groups_Post_Access', 'get_the_excerpt' ), 1 );
    		remove_filter( 'the_content', array( 'Groups_Post_Access', 'the_content' ), 1 );
    		if ( class_exists( 'Groups_Post_Access_Legacy' ) ) {
    			remove_filter( 'posts_where', array( 'Groups_Post_Access_Legacy', 'posts_where' ), 10, 2 );
    			remove_filter( 'the_posts', array( 'Groups_Post_Access_Legacy', 'the_posts' ), 1, 2 );
    			remove_filter( 'get_the_excerpt', array( 'Groups_Post_Access_Legacy', 'get_the_excerpt' ), 1 );
    			remove_filter( 'the_content', array( 'Groups_Post_Access_Legacy', 'the_content' ), 1 );
    		}
    	}
    }

    Please let me know if that helped – I suppose yes and will mark this thread as resolved, but please don’t hesitate to ask if you need further indications.

    Cheers

    This worked like a charm Kento. Thanks a bunch.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘pre_get_posts exclusion of groups rule has stopped working’ is closed to new replies.