WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] exclude pages by id in this script? (4 posts)

  1. jerrovicke
    Member
    Posted 1 year ago #

    Hi,

    I came across this piece of code to show some random pages in a little "related pages" section on a specific template. The problem is, this code chooses from every page on my website. I want to exclude pages such as my sitemap, contact page, ... from displaying because they are irrellevant. Any idea how and where i should adapt this code to exclude a couple of pages by id? It would be appreciated!

    working example:

    http://www.preciamolen.be/info/een-weegschaal-voor-iedere-sector/

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. Not the single quote, that doesn't work. ]

    <? $args=array(
      'orderby' => 'rand',
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => 3,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo '<div class="relatedpagesbox"><p>Bekijk ook onze andere producten en diensten:</p>';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p class="relatedpage"><a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    possibly add the paramter 'post__not_in' with an array of the to be excluded page IDs to the query args;

    http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

    example:

    <? $args=array(
      'orderby' => 'rand',
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => 3,
      'caller_get_posts'=> 1,
      'post__not_in' => array( 3,77,235 )
    );

    (untested)

  3. jerrovicke
    Member
    Posted 1 year ago #

    Would be great,

    i'll try it as soon as possible and leave a comment here!

    thx!

  4. jerrovicke
    Member
    Posted 1 year ago #

    @alchymyth thx for the tip, i just tested it and it seems to work! In attachement the full code. Don't forget to update the array for the post__not_in. Great piece of code to show some page links at random.

    <?php
    $args=array(
      'orderby' => 'rand',
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => 3,
      'caller_get_posts'=> 1,
      'post__not_in' => array( 2,22,30,24,27,406,418,1060,1687 )
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo '<div class="relatedpagesbox"><p>Bekijk ook onze andere producten en diensten:</p>';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p class="relatedpage"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic