Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Query loop: order by custom field

  • Hi there,

    I have both GB pro and GP premium.

    I have created a query loop and I’d like to set the parameters to order the quey loop output by an ACF custom field “idordine” (it’s a text custom field).

    I cannot see the right parameters to set.

    How can I do that?

    Thank you.

  • Hi there,

    I believe this article should help:
    https://docs.generateblocks.com/article/order-query-loop-by-custom-field/

    Let me know 🙂

  • Hi Leo,
    thank you. I managed to order the query loop according to the custom field.
    Now, I would like to filter another query loop by another custom field:

    I have a custom field called “highlighted”.
    It was created in ACF and it’s a “true/false” field.
    I want to filter the pages that have the “highlighted” field set to true.

    I tried some code that I found on this forum, but it’s not working.

    here’s an example I have tried with no success:

    	add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        // Check your logic to apply the filtering only to specific loop
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'testsliderfield' ) !== false &&
            is_home() &&
            ! is_admin()
        ) {
            // Merge the current $query_args which contains arguments defined in the editor with your ordering arguments
            return array_merge( $query_args, array(
                'meta_query' => array(
                    array(
                    'key' => 'highlighted',
                    'value' => '1',
                    'compare' => '==' 
                    )
                ),
            ) );
        }
    	return $query_args;
    }, 10, 2 );

    See the url in private information window.

    What am I missing?

    Thank you.

  • Hi Carlom,

    Try removing the Quotation marks for 1.

    Example: 'value' => 1,

  • Hi Fernando,

    I have just tried but it is still not working

  • Hi there,

    your snippet includes the is_home() && condition, if its not your home page then remove that condtion.

  • Hi David,

    you are right, I haven’t noticed that condition in the code I copied!

    Now it works.

    The code I am using is now this one:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    
    				// apply filter if loop has class: orderby-acf-idordine
    				if (! empty( $attributes['className'] ) && strpos( $attributes['className'], 'orderby-acf-idordine' ) !== false) {
    
    					return array_merge( $query_args, array(
                    'meta_key' => 'highlighted',
                    'meta_value' => '1',
                    'compare' => '==' 
    					));
    				}
    				return $query_args;
    			}, 10, 2 );

    How can I add the code to order by “idordine” field all the post that have “highlighted” = 1?

    I mean, how to combine the code above with this one in order to order by “idordine” field all the post that have “highlighted” = 1?

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    
    				// apply filter if loop has class: orderby-acf-idordine
    				if (! empty( $attributes['className'] ) && strpos( $attributes['className'], 'orderby-acf-idordine' ) !== false) {
    
    					return array_merge( $query_args, array(
    						'meta_key' => 'idordine',
    						'orderby' => 'meta_value',
    						'order' => 'ASC',
    					));
    				}
    				return $query_args;
    			}, 10, 2 );
  • Not 100% sure but you can try this:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        if (! empty( $attributes['className'] ) && strpos( $attributes['className'], 'orderby-acf-idordine' ) !== false) {
    
            return array_merge( $query_args, array(
                'meta_query' => array(
                    array(
                        'key' => 'highlighted',
                        'value' => '1',
                        'compare' => '='
                    )
                ),
                'orderby' => 'meta_value_num',
                'meta_key' => 'idordine',
                'order' => 'ASC'
            ));
        }
        
        return $query_args;
    
    }, 10, 2 );
  • Hi David,
    thank you, it works.

    This is the exact code that is working for me:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        if (! empty( $attributes['className'] ) && strpos( $attributes['className'], 'orderby-acf-idordine' ) !== false) {
    
            return array_merge( $query_args, array(
                'meta_query' => array(
                    array(
                        'key' => 'highlighted',
                        'value' => '1',
                        'compare' => '='
                    )
                ),
                'meta_key' => 'idordine',
    	    'orderby' => 'meta_value',
                'order' => 'ASC'
            ));
        }
        
        return $query_args;
  • Glad to be of help

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.