Tagged: acf, blog post element, custom, sort
-
AuthorPosts
-
July 29, 2021 at 5:18 pm #1313294
Dear Support-Team,
on many installations of your great theme I am using a code in my functions.php for sorting blog posts in a blog post element by custom queries. The code is from the support forum:if(!function_exists('avia_custom_query_extension')) { function avia_custom_query_extension($query, $params) { global $avia_config; if(!empty($avia_config['avia_custom_query_options']['order'])) { $query['order'] = $avia_config['avia_custom_query_options']['order']; } if(!empty($avia_config['avia_custom_query_options']['orderby'])) { $query['orderby'] = $avia_config['avia_custom_query_options']['orderby']; } unset($avia_config['avia_custom_query_options']); return $query; } add_filter('avia_masonry_entries_query', 'avia_custom_query_extension', 10, 2); add_filter('avia_post_grid_query', 'avia_custom_query_extension', 10, 2); add_filter('avia_post_slide_query', 'avia_custom_query_extension', 10, 2); add_filter('avia_blog_post_query', 'avia_custom_query_extension', 10, 2); add_filter('avf_magazine_entries_query', 'avia_custom_query_extension', 10, 2); add_filter('avf_template_builder_shortcode_elements','avia_custom_query_options', 10, 1); function avia_custom_query_options($elements) { $allowed_elements = array('av_blog','av_masonry_entries','av_postslider','av_portfolio','av_magazine'); if(isset($_POST['params']['allowed']) && in_array($_POST['params']['allowed'], $allowed_elements)) { $elements[] = array( "name" => __("Custom Query Orderby",'avia_framework' ), "desc" => __("Set a custom query orderby value",'avia_framework' ), "id" => "orderby", "type" => "select", "std" => "", "subtype" => array( __('Default Order', 'avia_framework' ) =>'', __('Title', 'avia_framework' ) =>'title', __('Random', 'avia_framework' ) =>'rand', __('Date', 'avia_framework' ) =>'date', __('Author', 'avia_framework' ) =>'author', __('Name (Post Slug)', 'avia_framework' ) =>'name', __('Modified', 'avia_framework' ) =>'modified', __('Comment Count', 'avia_framework' ) =>'comment_count', __('Page Order', 'avia_framework' ) =>'menu_order') ); $elements[] = array( "name" => __("Custom Query Order",'avia_framework' ), "desc" => __("Set a custom query order",'avia_framework' ), "id" => "order", "type" => "select", "std" => "", "subtype" => array( __('Default Order', 'avia_framework' ) =>'', __('Ascending Order', 'avia_framework' ) =>'ASC', __('Descending Order', 'avia_framework' ) =>'DESC')); } return $elements; } add_filter('avf_template_builder_shortcode_meta', 'avia_custom_query_add_query_params_to_config', 10, 4); function avia_custom_query_add_query_params_to_config($meta, $atts, $content, $shortcodename) { global $avia_config; if(empty($avia_config['avia_custom_query_options'])) $avia_config['avia_custom_query_options'] = array(); if(!empty($atts['order'])) { $avia_config['avia_custom_query_options']['order'] = $atts['order']; } if(!empty($atts['orderby'])) { $avia_config['avia_custom_query_options']['orderby'] = $atts['orderby']; } return $meta; } }
I am wondering if there is an option to set a field defined in ACF as sort query? The standard queries in the code are:
__('Default Order', 'avia_framework' ) =>'', __('Title', 'avia_framework' ) =>'title', __('Random', 'avia_framework' ) =>'rand', __('Date', 'avia_framework' ) =>'date', __('Author', 'avia_framework' ) =>'author', __('Name (Post Slug)', 'avia_framework' ) =>'name', __('Modified', 'avia_framework' ) =>'modified', __('Comment Count', 'avia_framework' ) =>'comment_count', __('Page Order', 'avia_framework' ) =>'menu_order')
Can I somehow add a ACF value and than sort by that value ASC. Somewhat like:
__('ACF', 'avia_framework' ) =>'acf_field_name',
Can you point me in any direction? That would be awesome!
Cheers, DanielAugust 2, 2021 at 12:33 pm #1313896Hey spooniverse,
Thank you for the inquiry.
We are not sure if this is going to work with ACF but you can sort entries based on their custom fields using the meta_key or meta_query parameter.
// https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/
You will have to add the parameter and create logic for it within the avia_custom_query_extension function.
Best regards,
IsmaelAugust 9, 2021 at 3:42 pm #1315872Hi Ismael,
thanks for the fast response and sorry for the late reply. So where exactly would I have to place the code? My field in ACF is called “last-name”, where would I have to call it? Inside the code above? This is in my functions.php at the moment.
Big thanks for any advice!
Cheers, DanielAugust 11, 2021 at 4:01 am #1316162Hi,
In the Custom Query Orderby settings, you can add the new option in the subtype parameter.
__('ACF Last Name', 'avia_framework' ) =>'acf_last_name')
This should add a new option in the drop down labeled ACF Last Name.
The logic should be added in the avia_custom_query_extension function. You could check if the $avia_config[‘avia_custom_query_options’][‘orderby’]) variable is set to acf_last_name. If it is set, set the meta_query parameter accordingly.
Something like:
if($avia_config['avia_custom_query_options']['orderby']) == "acf_last_name") { $query["meta_query"] = array( 'relation' => 'AND', 'state_clause' => array( 'key' => 'state', 'value' => 'Wisconsin', ), 'city_clause' => array( 'key' => 'city', 'compare' => 'EXISTS', ), ); $query['orderby'] = 'city_clause'; }
// https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/
Best regards,
Ismael -
AuthorPosts
- The topic ‘Sort Blog Post Element by "ACF" Advanced Custom Field’ is closed to new replies.