Advertisement
arie_cristianD

exclude sponsored posts new

Feb 15th, 2024 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. function get_sponsored_post_ids() {
  2.     $args = array(
  3.         'post_type'      => 'post',
  4.         'posts_per_page' => -1,
  5.         'fields'         => 'ids',
  6.         'meta_query'     => array(
  7.             array(
  8.                 'key'     => 'jnews_single_post',
  9.                 'value'   => '"sponsored_post";s:1:"1";',
  10.                 'compare' => 'LIKE',
  11.             ),
  12.         ),
  13.     );
  14.  
  15.     $query = new WP_Query( $args );
  16.     return $query->posts;
  17. }
  18. $exclude_posts = array();
  19. if ( ! is_admin() ) {
  20.     $exclude_posts = get_sponsored_post_ids();
  21. }
  22. $page_id = 0;
  23. function exclude_specific_posts( $query ) {
  24.     if ( $query->is_main_query() && $query->is_page() ) {
  25.         $new_page_id = $query->get_queried_object_id();
  26.         global $page_id;
  27.         $page_id = $new_page_id;
  28.     }
  29.     if ( ! is_admin() && ( $query->is_home() || $query->is_category() || $query->is_search() ) ) {
  30.         global $exclude_posts, $page_id;
  31.         /* change the 61 value with your home page id */
  32.  
  33.         if ( $query->is_home() && 61 == $page_id ) {
  34.             if ( ! empty( $query->query_vars['post__not_in'] ) ) {
  35.                 $excluded = array_unique( array_merge( $exclude_posts, $query->query_vars['post__not_in'] ) );
  36.                 $query->set( 'post__not_in', $excluded );
  37.             } else {
  38.                 $query->set( 'post__not_in', $exclude_posts );
  39.             }
  40.         } elseif ( $query->is_category() || $query->is_search() ) {
  41.             $query->set( 'post__not_in', $exclude_posts );
  42.         }
  43.     }
  44. }
  45. add_action( 'pre_get_posts', 'exclude_specific_posts', 99 );
  46.  
  47.  
  48.  
  49. function exclude_specific_posts_from_feed( $query ) {
  50.     if ( $query->is_feed() ) {
  51.         global $exclude_posts;
  52.         $query->set( 'post__not_in', $exclude_posts );
  53.     }
  54. }
  55. add_action( 'pre_get_posts', 'exclude_specific_posts_from_feed' );
  56.  
  57.  
  58. function disable_comments_rss() {
  59.     global $wp_rewrite;
  60.     $wp_rewrite->feeds = array_diff( $wp_rewrite->feeds, array( 'comments_rss2' ) );
  61. }
  62. add_action( 'init', 'disable_comments_rss', 1 );
  63.  
  64. function redirect_comments_rss_to_homepage() {
  65.     if ( is_comment_feed() ) {
  66.         wp_redirect( home_url(), 301 );
  67.         exit;
  68.     }
  69. }
  70. add_action( 'template_redirect', 'redirect_comments_rss_to_homepage' );
  71.  
  72.  
  73. add_filter( 'exclude_post_popular_widget', 'exc_post_popular_widget' );
  74. function exc_post_popular_widget( $exc ) {
  75.     global $exclude_posts;
  76.     $exc = implode( ',', $exclude_posts );
  77.     return $exc;
  78. }
  79.  
  80.  
  81. add_filter( 'jnews_ajax_attr_filters', 'exclude_in_ajax_module', 10, 2 );
  82.  
  83.  
  84. function exclude_in_ajax_module( $attr, $action ) {
  85.  
  86.     if ( 'jnews_module_ajax_jnews_block_4' == $action ) {
  87.         global $exclude_posts;
  88.             $exc                   = implode( ',', $exclude_posts );
  89.             $attr['exclude_post'] .= ",{$exc}";
  90.     }
  91.     return $attr;
  92. }
  93.  
  94. add_filter( 'jnews_get_content_attr', 'exclude_posts_in_author_page', 99, 3 );
  95.  
  96. function exclude_posts_in_author_page( $attr, $page, $author ) {
  97.  
  98.     if ( 'jnews_author_' == $page ) {
  99.         global $exclude_posts;
  100.             $exc                  = implode( ',', $exclude_posts );
  101.             $attr['exclude_post'] = $exc;
  102.     }
  103.     return $attr;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement