Forums

[resolved] Order Posts based on Value (8 posts)

  1. marques_uk
    Member
    Posted 7 months ago #

    Hi,

    I wonder if anyone can help me with a problem I'm having, I've been searching the internet for a few hours and I can't seem to find a simple solution to order my posts in a specific order.

    I've got this query that orders the posts based on meta values
    <?php query_posts("meta_key=number&orderby=meta_value"); ?>

    It orders my numbers but it lists the numbers from high to low, I'd like to reverse this so the posts start with the smallest number?

    Is there a simple solution?

    Thanks in advance
    Mark

  2. esmi
    Theme Diva & Forum Moderator
    Posted 7 months ago #

    Could it be that your numbers are being returned as strings? I've not tried it but would a meta-query work?

    $args = array(
    	'orderby' => 'meta_value',
    	'meta_query' => array(
    		array(
    			'key' => 'number',
    			'type' => 'number'
    		)
    	)
     );
    query_posts($args);
  3. marques_uk
    Member
    Posted 7 months ago #

    Hi esmi,

    No that doesn't seem to work either. It is querying the post as if you change the key name to one that doesn't exist no posts are displayed.

    The above code orders my numbers as 20.00, 40.00, 30.00?

    Any other suggestions?

    Thanks
    Mark

  4. esmi
    Theme Diva & Forum Moderator
    Posted 7 months ago #

    <?php query_posts("meta_key=number&orderby=meta_value_num"); ?>

  5. marques_uk
    Member
    Posted 7 months ago #

    That orders them 40.00, 30.00, 20.00. Need to reverse it somehow? :-)

  6. keesiemeijer
    moderator
    Posted 7 months ago #

    try:

    <?php query_posts("meta_key=number&orderby=meta_value_num&order=ASC"); ?>
  7. marques_uk
    Member
    Posted 7 months ago #

    That line of code worked perfect, thank you keesiemeijer! Thanks for your help as well esmi.

    I have another ordering problem that you might be able to help me with?

    I have the code
    <?php wp_list_pages('title_li='); ?>
    that displays the navigation list of pages, however, because the home page gets added first, it always comes last. I know you could just add the home page last but I'd like to add future pages to my website and I won't want to keep removing the home page and re-adding so it comes first.

    So at the moment I get Contact Testimonials Services Home.

    How can I adjust the code so the page Home always come first in the list? i'm not to bothered about the order of the other pages.

    I hope that makes sense?

    Thanks
    Mark

  8. marques_uk
    Member
    Posted 7 months ago #

    Oh it's OK, I'll just use the Page Attributes and make sure that Home is always 0.

    Thanks anyway
    Mark

Reply

You must log in to post.

About this Topic