rob-65-wp
Forum Replies Created
-
Thanks for your advice Kathryn.
I managed to sort my issue out with the help of these people/links – many thanks to them too
https://wordpress.stackexchange.com/questions/405588/using-pre-get-posts-on-a-specific-core-query-block to hook into function..<span class=”hljs-string”>
<code class="hljs language-bash"><span class="hljs-string">block_type_metadata_settings</span>
</span>which gave me access to the $block properties.Unfortunately this doesn’t provide access $query properties where I needed ‘meta_key’ etc.
Here https://docs.pods.io/code-snippets/ordering-cpt-archive-by-custom-fields/ helped
Finally sorting/filtering on date was done with the help of
https://wpfieldwork.com/modify-query-loop-block-to-filter-by-custom-field/
My code, which seems to work and I’m sure can be improved but I’m no expert ?? is …(I’m using a child theme – ie my modified functions.php file)
<?php ....... //call back wrapper for modifying ..'render_block_core_post_template' - rjb add_filter( 'block_type_metadata_settings', function( $settings ) { if ( $settings['parent'][0] !== 'core/query' ) { return $settings; } if ( $settings['render_callback'] !== 'render_block_core_post_template' ) { return $settings; } $settings['render_callback']= 'wpse_render_block_core_post_template'; return $settings; }); function wpse_render_block_core_post_template( $attributes, $content, $block ) { //queryIds unique per page , 7 = brochure posts if (( $block->context['queryId'] == 7 ) and ($block->context['query']['postType'] == 'post')){ add_action( 'pre_get_posts', 'order_query_by_custom_field_PIN' ); } //queryIds 70 = event posts if (( $block->context['queryId'] == 70 ) and ($block->context['query']['postType'] == 'post')){ add_action( 'pre_get_posts', 'order_query_by_custom_field_ED' ); } //queryIds 7 && CPT accommodation - never reached, left in incase need to sort CPTs if (( $block->context['queryId'] == 7 ) and ($block->context['query']['postType'] == 'accommodation') and (1==0)){ debug_to_console('postType?'); debug_to_console($block->context['query']['postType']); add_action( 'pre_get_posts', 'order_query_by_custom_field_TEST' ); } return render_block_core_post_template($fields, $content, $block ); } //PIN - post importance number function order_query_by_custom_field_PIN( $query ) { // same queryId may appear on other pages, this checks its the query on front page only if (is_main_query() && is_front_page()) { //set sort on meta_key post_importance_number $query->set( 'meta_key', 'post_importance_number' ); $query->set( 'order', 'desc' ); $query->set( 'orderby', 'meta_value_num' ); } } //ED - event date function order_query_by_custom_field_ED( $query ) { if (is_main_query() && is_front_page()) { // get today's date in Ymd format $today = date('Y-m-d'); $query->set( 'meta_value', $today ); $query->set( 'meta_compare', '>=' ); $query->set( 'meta_key', 'event_date' ); //show soonest events first $query->set( 'order', 'asc' ); $query->set( 'orderby', 'meta_value_date' ); } } //Test - to sort on custom fields on custom posts function order_query_by_custom_field_TEST( $query ) { if (is_main_query() && is_page('self-catering')) { //set sort on meta_key contact_name $query->set( 'meta_key', 'contact_name' ); $query->set( 'order', 'asc' ); //important to add this for sorting plain text! $query->set( 'orderby', 'meta_value' ); } } ?>
I’m trying to do something similar except I want to sort by a ‘custom field’, created with plugin PODs
my code is..
<!-- wp:query {"queryId":7,"query":{"perPage":"9","pages":0,"offset":0,"postType":"post","order":"asc","orderBy":"date",....
I’d like to change “date” to “event_date” but it just hangs?