• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Sort selected post categories descending by custom field, "price"

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › Sort selected post categories descending by custom field, "price"

This topic is: resolved

Tagged: custom field, pre_get_posts

  • This topic has 13 replies, 3 voices, and was last updated 6 years, 3 months ago by KarenR.
Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • February 24, 2020 at 11:23 am #496922
    KarenR
    Participant

    Hello,

    I am trying to sort selected post categories (IDs 5,6,15) by a custom field called price. The customer needs to enter this price, so the field includes the $ and commas. I found code to make the field numeric and unsigned, and I thought I had code to sort by descending price, but there is still a glitch.

    If there are three prices,
    $24,400,000
    $5,000,000
    $2,900,000
    then the largest one shows up as the smallest. How do I fix that, please?
    The current code:

    function func_orderby_asking_price( $orderby ) {
    global $WP_Views;

    if($WP_Views) {
    $orderby = str_replace( 'price', "cast(replace(trim( leading '$' from price),',','') AS UNSIGNED)", $orderby );
    }

    return $orderby;
    }
    add_filter('posts_orderby', 'func_orderby_asking_price' );

    add_action( 'pre_get_posts', 'kr_change_posts_order' );
    function kr_change_posts_order( $query ) {
    if ( $query->is_main_query() && !is_admin() ) {
    $query->set( 'cat', ( array( 5, 6, 15 ) ) );
    $query->set( 'orderby','meta_value' );
    $query->set( 'meta_key','price' );
    $query->set( 'order', 'DESC' );
    }
    }

    Any help appreciated. I'm planning for this site to go live tomorrow - if possible.

    Thank you,

    KarenR

    February 24, 2020 at 11:30 am #496923
    AnitaC
    Keymaster

    Hi @karenr what theme are you using?


    Need help with customization or troubleshooting? Reach out to me.

    February 24, 2020 at 12:11 pm #496926
    KarenR
    Participant

    Hi Anita,

    I'm using Education Pro.

    Karen

    February 24, 2020 at 12:48 pm #496927
    AnitaC
    Keymaster

    And where did you get your initial code to create that? More information on that would be helpful as well as how you implemented your code.


    Need help with customization or troubleshooting? Reach out to me.

    February 24, 2020 at 12:57 pm #496928
    KarenR
    Participant

    Hi Anita,

    This from a Toolset post:
    function func_orderby_asking_price( $orderby ) {
    global $WP_Views;

    if($WP_Views) {
    $orderby = str_replace( 'price', "cast(replace(trim( leading '$' from price),',','') AS UNSIGNED)", $orderby );
    }

    return $orderby;
    }
    // end
    This from Bill Erickson (shout out to Bill, thank you!) with some modifications:
    add_filter('posts_orderby', 'func_orderby_asking_price' );

    add_action( 'pre_get_posts', 'kr_change_posts_order' );
    function kr_change_posts_order( $query ) {
    if ( $query->is_main_query() && !is_admin() ) {
    $query->set( 'cat', ( array( 5, 6, 15 ) ) );
    $query->set( 'orderby','meta_value' );
    $query->set( 'meta_key','price' );
    $query->set( 'order', 'DESC' );
    }
    }
    //end
    I can't help but think that it is something simple that I am overlooking; the code seems to work EXCEPT for the fact that it sorts by the first digit, not by all digits.

    Karen

    February 24, 2020 at 1:17 pm #496930
    KarenR
    Participant

    I put the code in the functions.php file of the theme.

    Karen

    February 29, 2020 at 1:46 pm #497042
    KarenR
    Participant

    Hi, can anyone help with this, please?

    Karen

    February 29, 2020 at 7:52 pm #497043
    Brad Dalton
    Participant

    How do you test this without a copy of the plugin as toolset is a premium plugin?


    Tutorials for StudioPress Themes.

    February 29, 2020 at 8:20 pm #497044
    KarenR
    Participant

    Hi Brad, I found the code on a public-facing Toolset post. If global $WP_Views is not part of standard WP, then clearly that is part of the problem. But it did make a difference when I tried changing UNSIGNED to NUMERIC (while I was trying different things), so I thought it was part of core.

    Karen

    February 29, 2020 at 8:40 pm #497045
    KarenR
    Participant
    This reply has been marked as private.
    February 29, 2020 at 9:17 pm #497046
    Brad Dalton
    Participant

    I don't get private messages however you can contact me [email protected]

    $WP_Views is a global variable for your plugin and not a WordPress core global.

    Best practice would be to store the input value without any $ dollar sign or comma and then add these to the output. You can do that using number_format


    Tutorials for StudioPress Themes.

    February 29, 2020 at 11:59 pm #497047
    Brad Dalton
    Participant

    Using ACF doesn't allow you to save the $ dollar sign. If it did, a simple filter could be used to modify the saved value enabling you to successfully order by the price key.

    This is a filter they could also build into your plugin.


    Tutorials for StudioPress Themes.

    March 1, 2020 at 9:46 am #497050
    KarenR
    Participant

    Hi Brad,

    I don't have the Toolset plugin, I was just looking for code snippets online. I always search using "Genesis" and/or "Studiopress" but I wasn't having any luck. I also don't have ACF; I've just written into the functions.php file. I am not really a PHP programmer; I use and modify code snippets that you and others offer. (Which is greatly appreciated!)

    Originally it seemed that their code was helping but I just tested by taking it out and I see that it does nothing. Which leaves me with

    add_action( 'pre_get_posts', 'kr_change_posts_order' );
    function kr_change_posts_order( $query ) {
    if ( $query->is_main_query() && !is_admin() && !is_page() ) {
    $orderby = genesis_get_custom_field( 'price' );
    $query->set( 'cat', ( array( 5, 6, 15 ) ) );
    $query->set( 'meta_key', 'price' );
    $query->set( 'orderby', 'meta_value' );
    $query->set( 'order', 'DESC' );
    }
    }

    March 1, 2020 at 10:52 am #497051
    KarenR
    Participant

    Thanks to Brad Dalton for the solution. Adding it here in case it will help someone else in the future.

    This sorts posts by the custom field "price" in descending order, targeting only three categories with IDs of 5, 6 and 15.

    1. Install Advanced Custom Fields plugin by Eliot Condon. In the plugin setup, create a field called price, formatted as a number.

    2. Use the following code in functions.php:
    (This came from Bill Erickson, with some slight modification.)

    add_action( 'pre_get_posts', 'kr_change_posts_order' );
    function kr_change_posts_order( $query ) {
    if ( $query->is_main_query() && !is_admin() && !is_page() ) {
    $orderby = genesis_get_custom_field( 'price' );
    $query->set( 'cat', ( array( 5, 6, 15 ) ) );
    $query->set( 'meta_key', 'price' );
    $query->set( 'orderby', 'meta_value_num' );
    $query->set( 'order', 'DESC' );
    }
    }

    Thank you,
    Karen

  • Author
    Posts
Viewing 14 posts - 1 through 14 (of 14 total)
  • The topic ‘Sort selected post categories descending by custom field, "price"’ is closed to new replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble