Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to give priority to posts in the loop WordPress

  • SOLVED

hi there,
I would like to assign a value (custom field name is 'priority') from 1 to 9 to several posts (NOT to all posts) and to show them in DESC order in the loop with all other (without priority) posts. This could be useful for me to order the results giving more visibility (first positions) to posts I'm interested in.

I found this [[LINK href="http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query"]]function[[/LINK]] but it shows only posts with the custom filed setted and not all the other without custom field (priority).

Thanks in advance for your help.

Answers (3)

2011-08-01

Svilen Popov answers:

Why don't you make 2 loops - first with posts that have "priority" custom field, and second with the other posts.


advalue comments:

could please post an example? I tried without success...


Svilen Popov comments:

Try this
<?php
global $query_string;
query_posts($query_string . 'meta_key=priority&orderby=meta_value&order=DESC');
if (have_posts()) : while (have_posts()) : the_post();
?>
// display the posts
<?php endwhile; endif; wp_reset_query(); ?>


Svilen Popov comments:

Ops, put & before meta_key:
<blockquote>query_posts($query_string . '<strong>&</strong>meta_key=priority&orderby=meta_value&order=DESC');</blockquote>


Svilen Popov comments:

here is he full code, but it's not tested:
<?php
$priority_posts_ids = array();
global $query_string;
query_posts($query_string . 'meta_key=priority&orderby=meta_value&order=DESC');
if (have_posts()) : while (have_posts()) : the_post(); $priority_posts_ids[] = get_the_ID();
?>
// display the posts

<?php endwhile; endif; wp_reset_query(); ?>

<?php
$args = array(
'post__not_in' => $priority_posts_ids
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();
?>
// display the posts
<?php endwhile; endif; wp_reset_query(); ?>


advalue comments:

Ok I tried and I think we are on the right track... but all the other posts (without priority) are not showing... here you can find me index.php: http://pastebin.com/6FLfj3NF
thanks for your help!


Svilen Popov comments:

Try the latest edit. You'll have to get the priority posts IDs and exclude them from the second loop.


advalue comments:

I'm really sorry but I don't know where to put your cod in my page :(
As you can see form my code ([[LINK href="http://pastebin.com/6FLfj3NF"]][[/LINK]] I have <?php if (have_posts()) : ?> and <?php while (have_posts()) : the_post(); ?> separated in two different lines of cose... thanks for your help.


advalue comments:

my code here: http://pastebin.com/6FLfj3NF


Svilen Popov comments:

Try this - http://pastebin.com/hRcu2q5c


advalue comments:

It doesn't work :(

2011-08-01

Sébastien | French WordpressDesigner answers:

Hi, use this code


<?php


query_posts($query_string . 'meta_key=priority&orderby=meta_value_num&order=DESC');

if (have_posts()) : while (have_posts()) : the_post(); $anothers[] = get_the_ID();

the_title();
?>
</br>


<?php endwhile; endif; wp_reset_query(); ?>

here anothers posts

<?php

$args = array(

'post__not_in' => $anothers

);

query_posts($args);

if (have_posts()) : while (have_posts()) : the_post();

the_title();
?>

</br>

<?php endwhile; endif; wp_reset_query(); ?>


Sébastien | French WordpressDesigner comments:

as you can see, you must to use orderby=meta_value_num and no orderby=meta_value


advalue comments:

it seems to be working... thanks Sébastien !


Sébastien | French WordpressDesigner comments:

you're welcome !


advalue comments:

I confirm that it works... but if there no results in the loop it shows all the post instead to show a message like: Nothing found. Any idea? thanks


advalue comments:

I have tested better the script suggested and IT DOESN'T WORK in the loop. In fact in the pagination, the second page the results are the same of the first page and also the posts without custom filed are shown for each search or archive.
Anyone can help me? thanks.

2011-08-01

Jerson Baguio answers:




$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'priority'
ORDER BY wpostmeta.meta_value ASC ";
$myposts = $wpdb->get_results($querystr,OBJECT);

if ($myposts):


foreach ($myposts as $mypost):
// your posts here
endforeach;
endif;