• Resolved rob-65-wp

    (@rjbrugman)


    I’m hoping to substitute “orderBy”:”date” below with “orderBy”:”event_date” (a custom field from PODs add-in) to enable sorting, but it just hangs….

    <!-- wp:query {"queryId":7,"query":{"perPage":"9","pages":0,"offset":0,"postType":"post","order":"asc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false,"taxQuery":{"category":[61]}},"displayLayout":{"type":"flex","columns":3}} -->
    <div class="wp-block-query"><!-- wp:post-template -->

    the field is available on the page as….

    <!-- wp:pods/pods-block-field {"field":"event_date"} /--></div>

    any advice?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Kathryn Presner

    (@zoonini)

    Hi there @rjbrugman – I asked @greenshady to have a look at this, and here’s his guidance on how to proceed:

    WordPress isn’t aware of what event_date is, so it’s impossible to just plug it into orderBy (regardless of whether it’s the Query Loop block or a PHP-based WP_Query).

    This is relatively easy to do with a PHP WP_Query. Something like this:

    $query = WP_Query( [
    	'orderby'   => 'meta_value',
    	'meta_type' => 'DATE',
    	'meta_key'  => 'event_date'
    ] );

    That would then need to be transformed into a Query Loop block variation. Justin’s written a?full tutorial on Query Loop variations?on the Dev Blog, and says: “It’s a hefty topic, so some of that baseline knowledge is necessary to discuss solutions.” Once you have some Query Loop variation code to work from, if you need help diving in deeper, let me know.

    I hope this helps point you in the right direction to get started!

    Thread Starter rob-65-wp

    (@rjbrugman)

    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' );            
      }          
    }
    
    ?>
    
    
    
    Moderator Kathryn Presner

    (@zoonini)

    Glad you got it working! Thanks for providing your code in case it helps others. I’ll go ahead and mark this as resolved, but feel free to start a new thread if you need other help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sorting query loop in Gutenberg block based on custom field’ is closed to new replies.