0

I'd like to display a random page that:

  • has a parent page of X or Y or Z, etc.
  • excludes pages with the title "Contributors"

The following code works (though without the specific conditions above), but the_content isn't displaying anything. I'm not sure what's going wrong there.

fwiw, this is a template page for a child theme of Twenty Thirteen.

<?php
/*
Template Name: Random Page
*/

get_header(); ?>

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">

<?php
$args = array( 'post_type' => 'page', 'numberposts' => 1, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>

                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <header class="entry-header">

                        <h1 class="entry-title"><?php the_title(); ?></h1>
                    </header><!-- .entry-header -->

                    <div class="entry-content online-page">
                        <?php if($post->post_parent) {
    $parent_link = get_permalink($post->post_parent); ?>
<a href="<?php echo $parent_link; ?>" class="parent-link parent-header"><?php
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
?></a>
<?php } ?>


                <?php the_content(); ?>


                        <?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
                    </div><!-- .entry-content -->

                    <footer class="entry-meta">
                        <?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
                    </footer><!-- .entry-meta -->
                </article><!-- #post -->

                <?php twentythirteen_post_nav(); ?>

<?php endforeach; ?>
<?php wp_reset_query(); ?>


        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
1

The first condition you list is easy. You just need the post_parent__in arguments.

$args = array( 
  'post_type' => 'page', 
  'posts_per_page' => 1, 
  'orderby' => 'rand',
  'post_parent__in' => array(2,169), // replace with your IDs
);
$rand = new WP_Query($args);
if ($rand->have_posts()) {
  while ($rand->have_posts()) {
    $rand->the_post();
    the_title();
    echo '<br>';
  }
}

For the second condition, I believe you need a filter. WP_Query cannot handle that logic natively, so far as I know.

function restrict_post_name_wpse_130401($where) {
  remove_filter('posts_where','restrict_post_name_wpse_130401');
  return $where.' AND post_title != "Contributors"';
}
add_action('posts_where','restrict_post_name_wpse_130401');

$args = array( 
  'post_type' => 'page', 
  'posts_per_page' => 1, 
  'orderby' => 'rand',
  'post_parent__in' => array(2,169), // replace with your IDs
);
$rand = new WP_Query($args);

if ($rand->have_posts()) {
  while ($rand->have_posts()) {
    $rand->the_post();
    the_title();
    echo '<br>';
  }
}

Used exactly as above this should work fine. That is, if you add the filter immediately before your query it will run and remove itself, effecting only the one query.

0
-4

Try adding the page ID into the content e.g.

the_content($post->ID);
1
  • 4
    Why? The first argument for that function is $more_link_text. – fuxia Jan 20 '14 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.