Community Forums › Forums › Archived Forums › Design Tips and Tricks › Sort selected post categories descending by custom field, "price"
Tagged: custom field, pre_get_posts
- This topic has 13 replies, 3 voices, and was last updated 4 years, 6 months ago by KarenR.
-
AuthorPosts
-
February 24, 2020 at 11:23 am #496922KarenRParticipant
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 #496923AnitaCKeymasterFebruary 24, 2020 at 12:11 pm #496926KarenRParticipantHi Anita,
I'm using Education Pro.
Karen
February 24, 2020 at 12:48 pm #496927AnitaCKeymasterAnd 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 #496928KarenRParticipantHi 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 #496930KarenRParticipantI put the code in the functions.php file of the theme.
Karen
February 29, 2020 at 1:46 pm #497042KarenRParticipantHi, can anyone help with this, please?
Karen
February 29, 2020 at 7:52 pm #497043Brad DaltonParticipantHow do you test this without a copy of the plugin as toolset is a premium plugin?
February 29, 2020 at 8:20 pm #497044KarenRParticipantHi 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 #497045KarenRParticipantThis reply has been marked as private.February 29, 2020 at 9:17 pm #497046Brad DaltonParticipantI 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
February 29, 2020 at 11:59 pm #497047Brad DaltonParticipantUsing 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.
March 1, 2020 at 9:46 am #497050KarenRParticipantHi 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 #497051KarenRParticipantThanks 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 -
AuthorPosts
- The topic ‘Sort selected post categories descending by custom field, "price"’ is closed to new replies.