WordPress.org

Forums

Using WP_Query to display popular pages (15 posts)

  1. Kristof
    Member
    Posted 5 years ago #

    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.

  2. MichaelH
    Member
    Posted 5 years ago #

    <?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...

  3. Kristof
    Member
    Posted 5 years ago #

    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.

  4. MichaelH
    Member
    Posted 5 years ago #

    What about something simpler?

    wp_list_pages('child_of=3');
  5. Kristof
    Member
    Posted 5 years ago #

    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.

  6. MichaelH
    Member
    Posted 5 years ago #

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

  7. Kristof
    Member
    Posted 5 years ago #

    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.

  8. MichaelH
    Member
    Posted 5 years ago #

    And you've tried the child_of argument?

  9. Kristof
    Member
    Posted 5 years ago #

    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>
  10. MichaelH
    Member
    Posted 5 years ago #

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

  11. Kristof
    Member
    Posted 5 years ago #

    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>
  12. MichaelH
    Member
    Posted 5 years ago #

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

  13. Kristof
    Member
    Posted 5 years ago #

    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
    	}
    ?>
  14. MichaelH
    Member
    Posted 5 years ago #

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

    http://lesterchan.net/portfolio/programming/php/#wp-postviews

  15. Kristof
    Member
    Posted 5 years ago #

    Thanks - I'll check that out.

Topic Closed

This topic has been closed to new replies.

About this Topic