WordPress.org

Ready to get started?Download WordPress

Forums

Trying to order by a custom field (5 posts)

  1. bmc1416
    Member
    Posted 2 years ago #

    I have a custom field called "rank_points", currently a post either has a value of 40 or 1. I would like all of the posts with a value of 40 to show up in first by title alphabetically, then the posts with a value of 1 to show up by title and alphabetically.

    I have the following before the loop:

    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();			$wp_query>query('cat=20,-282&orderby=title&order=ASC'.'&paged='.$paged);

    Within my query can I insert 'meta_value=rank_points&order_by=rank_points'?

    Or do I have to query the meta values and then order by the results?

  2. popper
    Member
    Posted 2 years ago #

  3. bmc1416
    Member
    Posted 2 years ago #

    When I add the meta_value I get no results:

    $wp_query->query('cat=20,282&meta_value=rank_points&orderby=meta_value_num'.'&paged='.$paged);

    but when I remove it and use

    $wp_query->query('cat=20,282&orderby=title&order=ASC'.'&paged='.$paged);

    I get the results as I would expect.

    I'm missing something when I add &meta_value=rank_points&orderby=meta_value_num

  4. bmc1416
    Member
    Posted 2 years ago #

    Popper thank you for leading me in the right direction. The following worked for me:

    $wp_query->query('cat=20&meta_key=rank_points&orderby=meta_value_num'.'&paged='.$paged);

    I forgot to call "meta_key".

  5. popper
    Member
    Posted 2 years ago #

    Glad you figured out :).

Topic Closed

This topic has been closed to new replies.

About this Topic