Forcing page to top of the list when using get_pages

I am using get_pages to pull the children pages from a parent and listing them in a similar way to the standard WP loop for pages. This works well using the code referenced below - however.. what I would like to be able to do is to list a page I know the ID for at the top irrespective of its publish date (I am listing the pages in date order). Is this possible? Page ID of the page I want to always be listed first is #196.

Thanks

?php
$ids = array();
$pages = get_pages(child_of=.$post-ID);
if ($pages) {
    foreach ($pages as $page) {
        $ids[] = $page-ID;
    }
}
$paged = (get_query_var(paged)) ? get_query_var(paged) : 1;
$args = array(
    paged = $paged,
    post__in = $ids,
    posts_per_page = 100,
    post_type = page
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?
div class=feedItemWrapper wpb_animate_when_almost_visible wpb_fadeInUp fadeInUp
a href=?php the_permalink(); ? title=?php the_title(); ?
?php echo get_the_post_thumbnail( $post_id, 'full', array() ); ?
div class=feedItemContentWrapper
h3?php the_title(); ?/h3
p?php the_excerpt(); ?/p
p class=date?php echo get_the_date(); ?/p
/div
/a

Topic loop Wordpress

Category Web


If you look at your first query to find the IDs you will see that you create a list of $ids in the order that get_pages() returns. Default to this is 'post_title'.

So if you pass a sort_column to this query you can get this to return in date order.

$ids   = array();
$pages = get_pages(
    array(
        'child_of'    => $post->ID,
        'sort_order'  => 'DESC', // Choose your sort options in your first query args
        'sort_column' => 'post_date',
    )
);
if ( $pages ) {
    foreach ( $pages as $page ) {
        $ids[] = $page->ID;
    }
}

Then just inject your chosen post in the begining of that $ids array

$ids   = array_unshift( $ids, 196 );

Now sort your posts by the order your IDs are in

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args  = array(
    'paged'          => $paged,
    'post__in'       => $ids,
    'posts_per_page' => 100,
    'post_type'      => 'page',
    'orderby'        => 'post__in',
);
query_posts( $args );
if ( have_posts() ) :
    while ( have_posts() ) :
        the_post();
        // etc 

if the result of query_posts($args) is the array of which you want to modify the order, something like that could possibly work:

$the_posts = query_posts($args);

$mypost = [];
for ($i=0;$i<sizeof($the_posts);$i++){
    if ($the_posts[$i]['id'] === '196'):
        $mypost = $the_posts[$i];
        unset($the_posts[$i]);
    endif;
} 

if(!empty($mypost)):
    array_unshift($the_posts,$mypost);
endif;

//your loop with the new post array
foreach $the_posts as $post_iteration:
    setup_postdata($post_iteration);

    the_post_thumbnail()....
endforeach;
wp_reset_postdata();

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.