$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Exclude posts in home slider from sections|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Exclude posts in home slider from sections

matteradmin7PV0评论

I have home page with a main slider and news section.

  • Main slider
  • Sport section news
  • Economy section news
  • Politic section news
  • and others section

The main slider get posts by tag ='slider', and display 10 posts limit.

The sections get posts by category (each section by specific category)

What i wold like is to : exclude the posts displaying in slider, from the sections until the posts is out the slider.

Here is the code of the slider and a sample of section:

Slider Code :

<?php
// Slider
$tag = ap_option('slider_tag');
$count = ap_option('slider_count');
$slid = new WP_Query(
array(
'tag'            => $tag ,
'posts_per_page' => $count ,
'post__not_in'   => get_option( 'sticky_posts' ),
)
);
?>
<div class="thumbslider">
<div class="postlist">
<?php while ( $slid->have_posts() ) : $slid->the_post(); ?>
<div class="item">
<div class="det">
<h2><a href="<?php the_permalink() ?>" title="<?php the_title();?>"><?php 
the_title(); ?></a></h2>
        </div>
        <div class="thumbnail">
          <a href="<?php the_permalink() ?>" title="<?php the_title();?>">
            <?php 
            if ( has_post_thumbnail() ) { 
              the_post_thumbnail( 'slider' );
            } else {
              echo '<img alt="Assahifa" title="Assahifa" src=";bg=eee&txtclr=8C8C8C%26text%3Dthree&txt=ap&w=650&h=420">';
            } 
            ?>
          </a>
        </div>
      </div>
      <?php endwhile;wp_reset_postdata(); ?>
    </div>
    </div>

Section News Sample :

   <?php
   // cat eco
   $n=0;
   $cat_eco = ap_option('sec_eco_cat');
   $count_eco = ap_option('sec_eco_count');
   $eco = new WP_Query(
   array(
   'cat'            => $cat_eco ,
   'posts_per_page' => $count_eco ,
   'post__not_in'   => get_option( 'sticky_posts' ),
   'offset'         => 0,
     )
   );
   ?>
   <section class="main_eco clearfix">
   <div class="box">
   <div class="title" style="color: <?php echo ap_option('eco-color'); ?>;"> 
   <h3 style="color: <?php echo ap_option('eco-color'); ?>;"><a href="<?php  
   echo get_category_link( $cat_eco ); ?>" title="<?php echo 
   ap_option('sec_eco_title'); ?>" style="color: <?php echo ap_option('eco- 
   color'); ?>;"><?php echo ap_option('sec_eco_title'); ?></a></h3></div>
   <div class="postlist">
      <?php while ( $eco->have_posts() ) : $eco->the_post();$n++; ?>
      <div class="item clearfix">
        <div class="thumbnail imgtrans">
          <a href="<?php the_permalink() ?>" title="<?php the_title();?>">
            <?php 
            if ( has_post_thumbnail() ) { 
              the_post_thumbnail( 'md' );
            } else {
              echo '<img alt="AisPanel" title="AisPanel" 
      src="? 
      txtsize=20&bg=eee&txtclr=8C8C8C%26text%3Dthree&txt=ap&w=650&h=420">';
            } 
            ?>
          </a>
        </div>
        <div class="det">
          <h2><a href="<?php the_permalink() ?>" title="<?php the_title();? 
       >"><?php the_title(); ?></a></h2>
          <!--<?php if($n == 1) the_excerpt(); ?>-->
        </div>
      </div>
      <?php endwhile;wp_reset_postdata(); ?>
      </div>
    </div>
    </section>

I have home page with a main slider and news section.

  • Main slider
  • Sport section news
  • Economy section news
  • Politic section news
  • and others section

The main slider get posts by tag ='slider', and display 10 posts limit.

The sections get posts by category (each section by specific category)

What i wold like is to : exclude the posts displaying in slider, from the sections until the posts is out the slider.

Here is the code of the slider and a sample of section:

Slider Code :

<?php
// Slider
$tag = ap_option('slider_tag');
$count = ap_option('slider_count');
$slid = new WP_Query(
array(
'tag'            => $tag ,
'posts_per_page' => $count ,
'post__not_in'   => get_option( 'sticky_posts' ),
)
);
?>
<div class="thumbslider">
<div class="postlist">
<?php while ( $slid->have_posts() ) : $slid->the_post(); ?>
<div class="item">
<div class="det">
<h2><a href="<?php the_permalink() ?>" title="<?php the_title();?>"><?php 
the_title(); ?></a></h2>
        </div>
        <div class="thumbnail">
          <a href="<?php the_permalink() ?>" title="<?php the_title();?>">
            <?php 
            if ( has_post_thumbnail() ) { 
              the_post_thumbnail( 'slider' );
            } else {
              echo '<img alt="Assahifa" title="Assahifa" src="https://placeholdit.imgix/~text?txtsize=20&bg=eee&txtclr=8C8C8C%26text%3Dthree&txt=ap&w=650&h=420">';
            } 
            ?>
          </a>
        </div>
      </div>
      <?php endwhile;wp_reset_postdata(); ?>
    </div>
    </div>

Section News Sample :

   <?php
   // cat eco
   $n=0;
   $cat_eco = ap_option('sec_eco_cat');
   $count_eco = ap_option('sec_eco_count');
   $eco = new WP_Query(
   array(
   'cat'            => $cat_eco ,
   'posts_per_page' => $count_eco ,
   'post__not_in'   => get_option( 'sticky_posts' ),
   'offset'         => 0,
     )
   );
   ?>
   <section class="main_eco clearfix">
   <div class="box">
   <div class="title" style="color: <?php echo ap_option('eco-color'); ?>;"> 
   <h3 style="color: <?php echo ap_option('eco-color'); ?>;"><a href="<?php  
   echo get_category_link( $cat_eco ); ?>" title="<?php echo 
   ap_option('sec_eco_title'); ?>" style="color: <?php echo ap_option('eco- 
   color'); ?>;"><?php echo ap_option('sec_eco_title'); ?></a></h3></div>
   <div class="postlist">
      <?php while ( $eco->have_posts() ) : $eco->the_post();$n++; ?>
      <div class="item clearfix">
        <div class="thumbnail imgtrans">
          <a href="<?php the_permalink() ?>" title="<?php the_title();?>">
            <?php 
            if ( has_post_thumbnail() ) { 
              the_post_thumbnail( 'md' );
            } else {
              echo '<img alt="AisPanel" title="AisPanel" 
      src="https://placeholdit.imgix/~text? 
      txtsize=20&bg=eee&txtclr=8C8C8C%26text%3Dthree&txt=ap&w=650&h=420">';
            } 
            ?>
          </a>
        </div>
        <div class="det">
          <h2><a href="<?php the_permalink() ?>" title="<?php the_title();? 
       >"><?php the_title(); ?></a></h2>
          <!--<?php if($n == 1) the_excerpt(); ?>-->
        </div>
      </div>
      <?php endwhile;wp_reset_postdata(); ?>
      </div>
    </div>
    </section>
Share Improve this question edited Jan 31, 2019 at 17:34 Zawaya Vision asked Jan 31, 2019 at 16:54 Zawaya VisionZawaya Vision 12 bronze badges 5
  • All codes are written in one single PHP file? – Chinmoy Kumar Paul Commented Jan 31, 2019 at 17:14
  • If all loops are written in same file, then you can apply do_not_duplicate idea here. – Chinmoy Kumar Paul Commented Jan 31, 2019 at 17:20
  • No , each code is separated: main-slider.php & section-eco.php – Zawaya Vision Commented Jan 31, 2019 at 17:30
  • How are you getting $slid_post variable in news section file? are you using the get_template_part() function in main PHP file (home.php/index.php) – Chinmoy Kumar Paul Commented Jan 31, 2019 at 17:33
  • Oops i just remember! in the origin it's was 'get_option( 'sticky_posts' ),'-- that was me trying some solution, i just edit the code again – Zawaya Vision Commented Jan 31, 2019 at 17:36
Add a comment  | 

2 Answers 2

Reset to default 0

I am assuming that you are saving the slider code in slider.php file, news section code in section-news.php file and following this kind of snippets in front-page/home/index .php file

get_template_part('slider');
get_templates_part('section-news');

then you can edit the slider.php file and add the new array variable $do_not_duplicate. So it is preserving the slider post IDs for future use. Here is the new code for slider

<?php
// Slider
$tag = ap_option('slider_tag');
$count = ap_option('slider_count');
$do_not_duplicate = array();
$slid = new WP_Query(
  array(
  'tag'            => $tag ,
  'posts_per_page' => $count ,
  'post__not_in'   => get_option( 'sticky_posts' ),
  )
);
?>
<div class="thumbslider">
  <div class="postlist">
    <?php 
      while ( $slid->have_posts() ) : 
        $slid->the_post(); 
        $do_not_duplicate[] = get_the_ID();
    ?>
      <div class="item">
        <div class="det">
          <h2><a href="<?php the_permalink() ?>" title="<?php the_title();?>"><?php the_title(); ?></a></h2>
        </div>
        <div class="thumbnail">
            <a href="<?php the_permalink() ?>" title="<?php the_title();?>">
              <?php 
              if ( has_post_thumbnail() ) { 
                the_post_thumbnail( 'slider' );
              } else {
                echo '<img alt="Assahifa" title="Assahifa" src="https://placeholdit.imgix/~text?txtsize=20&bg=eee&txtclr=8C8C8C%26text%3Dthree&txt=ap&w=650&h=420">';
              } 
              ?>
            </a>
          </div>
        </div>
      <?php endwhile;wp_reset_postdata(); ?>
    </div>
</div>

Now I shall exclude the slider post from news loop. Here is the new code for news section. Merging $do_not_duplicate array with get_option( 'sticky_posts' ) value and passing into post__not_in array key.

<?php
   // cat eco
   $n=0;
   $cat_eco = ap_option('sec_eco_cat');
   $count_eco = ap_option('sec_eco_count');
   $sticky_posts = ( get_option( 'sticky_posts' ) ) ? get_option( 'sticky_posts' ) : array();
   $eco = new WP_Query(
     array(
       'cat'            => $cat_eco ,
       'posts_per_page' => $count_eco ,
       'post__not_in'   => array_merge( $do_not_duplicate, $sticky_posts ),
       'offset'         => 0,
      )
   );
   ?>
   <section class="main_eco clearfix">
   <div class="box">
   <div class="title" style="color: <?php echo ap_option('eco-color'); ?>;"> 
   <h3 style="color: <?php echo ap_option('eco-color'); ?>;"><a href="<?php  
   echo get_category_link( $cat_eco ); ?>" title="<?php echo 
   ap_option('sec_eco_title'); ?>" style="color: <?php echo ap_option('eco- 
   color'); ?>;"><?php echo ap_option('sec_eco_title'); ?></a></h3></div>
   <div class="postlist">
      <?php while ( $eco->have_posts() ) : $eco->the_post();$n++; ?>
      <div class="item clearfix">
        <div class="thumbnail imgtrans">
          <a href="<?php the_permalink() ?>" title="<?php the_title();?>">
            <?php 
            if ( has_post_thumbnail() ) { 
              the_post_thumbnail( 'md' );
            } else {
              echo '<img alt="AisPanel" title="AisPanel" 
      src="https://placeholdit.imgix/~text? 
      txtsize=20&bg=eee&txtclr=8C8C8C%26text%3Dthree&txt=ap&w=650&h=420">';
            } 
            ?>
          </a>
        </div>
        <div class="det">
          <h2><a href="<?php the_permalink() ?>" title="<?php the_title();? 
       >"><?php the_title(); ?></a></h2>
          <!--<?php if($n == 1) the_excerpt(); ?>-->
        </div>
      </div>
      <?php endwhile;wp_reset_postdata(); ?>
      </div>
    </div>
    </section>

With a friend we find a solution : - we create a exclude function in functions.php
- then we call it where we went

This the code for functions.php

    // Function exlude
        if( !function_exists('get_slide_ids') ){
        function get_slide_ids() {  
        global $wp_query;
        $POST_LIMIT = ap_option('slider_count');
        $POST_TAG = ap_option('slider_tag');
        $ids= [];
        $args =  array(
                    'post_status' => 'publish',
                    'tag'            => $POST_TAG ,
                    'posts_per_page' => $POST_LIMIT ,
                );
        $wp_query = new WP_Query($args);
        if ($wp_query->have_posts() ):
            while ($wp_query->have_posts()) : $wp_query->the_post();
                  $ids[] = get_the_ID(); 
            endwhile;   
            return $ids;
        endif; 
        return '';
        }
     }

To call it add the syntaxe not in, this my code just try a adopt it to yours :

     <?php
      /**
      // cat eco
      $n=0;
      $cat_eco = ap_option('sec_eco_cat');
      $count_eco = ap_option('sec_eco_count');
      $sticky_posts = ( get_option( 'sticky_posts' ) ) ? get_option( 'sticky_posts' ) : array();
      $eco = new WP_Query(
      array(
        'cat'            => $cat_eco ,
        'posts_per_page' => $count_eco ,
        'post__not_in'   => get_slide_ids(),
           )
         );
       ?>
Post a comment

comment list (0)

  1. No comments so far