Forum Replies Created
-
AuthorPosts
-
March 1, 2020 at 10:52 am in reply to: Sort selected post categories descending by custom field, "price" #497051KarenRParticipant
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,
KarenMarch 1, 2020 at 9:46 am in reply to: Sort selected post categories descending by custom field, "price" #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' );
}
}February 29, 2020 at 8:40 pm in reply to: Sort selected post categories descending by custom field, "price" #497045KarenRParticipantThis reply has been marked as private.February 29, 2020 at 8:20 pm in reply to: Sort selected post categories descending by custom field, "price" #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 1:46 pm in reply to: Sort selected post categories descending by custom field, "price" #497042KarenRParticipantHi, can anyone help with this, please?
Karen
February 24, 2020 at 1:17 pm in reply to: Sort selected post categories descending by custom field, "price" #496930KarenRParticipantI put the code in the functions.php file of the theme.
Karen
February 24, 2020 at 12:57 pm in reply to: Sort selected post categories descending by custom field, "price" #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 12:11 pm in reply to: Sort selected post categories descending by custom field, "price" #496926KarenRParticipantHi Anita,
I'm using Education Pro.
Karen
July 10, 2017 at 5:54 pm in reply to: Trying to make featured posts horizontal with Education Pro #208937KarenRParticipantHa! I knew it was just one thing I wasn't seeing. Thank you so very much; I can get the rest from here.
All the best in your endeavors.
Karen
July 10, 2017 at 4:15 pm in reply to: Trying to make featured posts horizontal with Education Pro #208929KarenRParticipantSorry, typo, I meant "but they will only line up vertically"
Karen
-
AuthorPosts