WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Order posts by Cat and numerical Custom Fields (6 posts)

  1. bizwoo
    Member
    Posted 4 years ago #

    Hi
    I have been trying to order posts from one category in desc or asc order depending on a numerical value in a custom field.
    I believe that wordpress 3.0 have orderby=meta_value_num implemented which should order post using the values in a custom field by-passing previous problems due to sql considering custom field values as strings and not integers.

    This is the query I am using:
    query_posts("cat=$current_id&meta_key=rated&orderby=meta_value_num&order=DESC");
    But I do not get the posts ordered as supposed when using this query.
    Any help?

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    Try:

    query_posts("cat=" . $current_id . "&meta_key=rated&orderby=meta_value_num&order=DESC");

  3. Temi4sh
    Member
    Posted 3 years ago #

    Hello

    $sel = array(
    		"post_type"=>"page",
    		"posts_per_page"=>2,
    		"meta_key"=>"position",
    		"post_status"=>"publish",
    		"orderby"=>"meta_value_num",
    		"order"=>"ASC",
    	);
    query_posts($sel);

    not orderBy position number(((

    why?

  4. esmi
    Forum Moderator
    Posted 3 years ago #

    'meta_value_num' - Order by numeric meta value (available with Version 2.8). Also note that a 'meta_key=keyname' must also be present in the query. This value allows for numerical sorting as noted above in 'meta_value'.

    http://codex.wordpress.org/Function_Reference/WP_Query#Order_.26_Orderby_Parameters

  5. Temi4sh
    Member
    Posted 3 years ago #

    I read this, but I present in my query "meta_key"=>"position", but it doesn't work.

  6. Temi4sh
    Member
    Posted 3 years ago #

    thanks, I find my error

Topic Closed

This topic has been closed to new replies.

About this Topic