2
votes

I building one plugin what need to allow admins to exclude users from reading some special contents using post ID and custom post type.

Generaly, if some post have ID 99 with custom post type lesions and admin choose that to exclude, user get 404 if go on direct link and also not see that post in list.

NOTE: I don't need to made loop, I need to somehow affect with other plugin on WP_Query with particular custom post type via some hook, filter or action.

1

1 Answers

1
votes

I did not get the time to test it yet. But it should be working. The idea is to save the post ids and post types in wp_options. Then you can get those values and save them in an array. After that you compare them with current post and do the action that you want. You can put this in functions.php

function sr_excluded_users()
{
    global $post;
    global $wp_query;
    $exclusive_post_ids = array(); // List of post ids ( you can store it in wp options )
    $exclusive_post_types = array(); // List of custom post types ( you can store it in wp options )
    if( in_array( $post->ID, $exclusive_post_ids ) && in_array( $post->post_type, $exclusive_post_types ) )
    {        
        $wp_query->set_404();
        status_header( 404 );
        get_template_part( 404 ); 
        exit();
    }
}
add_action( 'wp', 'sr_excluded_users' );

Edit : Function to remove posts from loop

function sr_excluded_users_loop($query) 
{
    $exclusive_post_ids = array(); // List of post ids ( you can store it in wp options )
    $query->set( 'post__not_in' , $exclusive_post_ids );
}
add_action( 'pre_get_posts', 'sr_excluded_users_loop' );