Question

I'm using the query_posts for "Page" item rather than "Post". I want to have the ability to make the "featured" page always on the top, we could add a custom field called "featured_product", if it's eq "1" then display the post as the very first one.

Here is the basic code for the query. Someone help please!?

<?php query_posts(array('showposts' => 1000, 'post_parent' => $post->ID, 'post_type' => 'page', 'orderby' => 'title', 'order' => 'ASC')); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...                             
<?php endwhile; else: ?>
...
<?php endif; ?>
Was it helpful?

Solution

In your query posts array add this:

'meta_key' => (meta key name in database), 'meta_value' => 1

To make it at the top use two queries. One for the top first post and one for the rest of the posts/pages. so your whole query will look like this:

<?php query_posts(array('showposts' => 1, 'post_parent' => $post->ID, 'post_type' => 'page', 'orderby' => 'title', 'order' => 'ASC', 'meta_key' => featured, 'meta_value' => 1); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...                             
<?php endwhile; else: ?>
...
<?php endif; ?>
<?php wp_reset_query(); ?>
<second query goes here as you have it in your post>

Replace "featured" with your key name.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top