WordPress.org

Support

Support » How-To and Troubleshooting » Using WP_Query to display popular pages

Using WP_Query to display popular pages

  • I’m trying to display popular pages (of a parent) in a sidebar based on page views — like popular posts, but for pages.

    I’ve tried the following code – which does list pages – but it’s not listing child pages of page ID 3.

    <?php $popular = new WP_Query('static=true &post_type=page &get_children=3 &post_status=publish &orderby=ASC &posts_per_page=5'); ?>
    
    <h5>Popular Pages</h5>
    <ul>
    <?php while ($popular->have_posts()) : $popular->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>

    I’ve also tried using
    &post_parent=3

    Any suggestions would be greatly appreciated.

Viewing 14 replies - 1 through 14 (of 14 total)
  • <?php
    $args=array(
      'post_parent' => 3,
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of child pages';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><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().
    ?>

    Of course that doesn’t really address the ‘popular’ aspect…

    Michael –

    Thank you so much for the quick help and code! It works great to display children of the primary page ID but I also need to include 3rd level sub-pages into the equation (which I forget to include in my question).

    Such as;

    Primary (Services) Page
    – 2nd level (section pages)
    — 3rd level pages (need to included these pages too)

    I tried adding additional IDs to post_parent but didn’t seem to work. Suggestion?

    Thanks Again.

    What about something simpler?

    wp_list_pages('child_of=3');

    That does work – and I do use that for listing sub-pages on specific services pages – but I’m trying to display a dynamic list of popular services (pages) based on page views. Just like a popular posts plugin – but for pages. Unfortunately, no one has created anything just for pages.

    Hopefully that gives you more info.

    Thank you.

    Again, assuming you can identify what is POPULAR then look at
    Function_Reference/get_pages

    Yah, that would be the trick. I’ve gone through the get_pages functions and can’t find a way to make it work. Then again, I’m fairly on the novice php coding side. I’m beginning to wonder if it’s even possible.

    And you’ve tried the child_of argument?

    I have. It lists all pages and sub-pages of the whole site – not just those of the main parent ID.

    <?php
    $args=array(
      'child_of' => 3,
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo '<h5>Popular Services</h5><ul>';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></a></li>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?></ul>

    Sorry, I meant have you tried the child_of with get_pages?

    My hack at using get_pages isn’t working – but I’m tryin’

    <?php
    $pages = get_pages('
    	child_of=3
    	&post_type=page
    	&post_status=publish
    	&orderby=ASC
    	&posts_per_page=5'
    );
    $my_query = null;
    $my_query = new WP_Query($pages);
    if( $my_query->have_posts() ) {
      echo '<h5>Popular</h5><ul>';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></a></li>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?></ul>

    Better look at the examples in the Function_Reference/get_pages–you won’t need the query posts thing

    Thanks for the ref but this is falls outside my coding experience.

    I used the below but It’s only listing sub-pages from one page ID and there doesn’t seem to be an option for sorting by page views.

    <?php
    	$pages = get_pages('child_of=3 &sort_order=desc');
    	$count = 0;
    	foreach($pages as $page)
    	{
    		$content = $page->post_content;
    		if(!$content)
    			continue;
    		if($count >= 5)
    			break;
    		$count++;
    		$content = apply_filters('the_content', $content);
    	?>
    		<li><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></li>
    	<?php
    	}
    ?>

    That’s what I was saying about “popular”. You need to be using a plugin to be keeping track of page views.

    Pringles Party Speaker

    Thanks – I’ll check that out.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Using WP_Query to display popular pages’ is closed to new replies.
Skip to toolbar