mirror of
https://github.com/10h30/ultimatemember.git
synced 2026-06-05 15:09:37 +09:00
Merge pull request #856 from ultimatemember/fix/exclude_posts_from_loop
Exclude posts from a query (wordpress.org support)
This commit is contained in:
@@ -55,6 +55,7 @@ if ( ! class_exists( 'um\core\Access' ) ) {
|
|||||||
//there also will be redirects if they need
|
//there also will be redirects if they need
|
||||||
//protect posts types
|
//protect posts types
|
||||||
add_filter( 'the_posts', array( &$this, 'filter_protected_posts' ), 99, 2 );
|
add_filter( 'the_posts', array( &$this, 'filter_protected_posts' ), 99, 2 );
|
||||||
|
add_filter( 'pre_get_posts', array( &$this, 'exclude_posts' ), 99, 1 );
|
||||||
//protect pages for wp_list_pages func
|
//protect pages for wp_list_pages func
|
||||||
add_filter( 'get_pages', array( &$this, 'filter_protected_posts' ), 99, 2 );
|
add_filter( 'get_pages', array( &$this, 'filter_protected_posts' ), 99, 2 );
|
||||||
//filter menu items
|
//filter menu items
|
||||||
@@ -1013,6 +1014,33 @@ if ( ! class_exists( 'um\core\Access' ) ) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exclude posts from query
|
||||||
|
*
|
||||||
|
* @param $query
|
||||||
|
*/
|
||||||
|
function exclude_posts( $query ) {
|
||||||
|
if( $query->is_main_query() ){
|
||||||
|
global $wpdb;
|
||||||
|
$exclude_posts = array();
|
||||||
|
$posts = $wpdb->get_col("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'um_content_restriction'");
|
||||||
|
foreach ( $posts as $post ) {
|
||||||
|
$content_restriction = get_post_meta( $post, 'um_content_restriction', true );
|
||||||
|
|
||||||
|
if ( is_user_logged_in() && $content_restriction['_um_custom_access_settings'] == 1 && $content_restriction['_um_accessible'] == 1 ) {
|
||||||
|
array_push( $exclude_posts, $post );
|
||||||
|
}
|
||||||
|
if ( ! is_user_logged_in() && $content_restriction['_um_custom_access_settings'] == 1 && $content_restriction['_um_accessible'] == 2 ) {
|
||||||
|
array_push( $exclude_posts, $post );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$query->set('post__not_in', $exclude_posts );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $single_template
|
* @param string $single_template
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user