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 order by custom field

  • I am following the instructions here : https://docs.generateblocks.com/article/order-query-loop-by-custom-field/
    But can’t seem to get ordering by an ACF field working.

    Here is my code:

    add_filter( ‘generateblocks_query_loop_args’, function( $query_args, $attributes ) {
    // apply filter if loop has class: my-class-name
    if (
    ! empty( $attributes[‘className’] ) &&
    strpos( $attributes[‘className’], ‘in-date-order’ ) !== false
    ) {
    // merge meta_key my-custom-field into query
    return array_merge( $query_args, array(
    ‘meta_key’ => ‘event_date’,
    ‘orderby’ => ‘meta_value’,
    ‘meta_type’ => ‘DATE’,
    ‘order’ => ‘ASC’,
    ) );
    }

    return $query_args;

    }, 10, 2 );

    I am using an ACF datepicker field.

  • Hi there,

    It should work, did you add the in-date-order class to the Grid block of the query loop?

    Do you have order/order by parameter added to the query loop? If so, make sure you remove it.

    Let me know 🙂

  • Thanks! I figured it out, it was just that someone had enabled the ‘Post Types Order’ plugin and that was messing things up.

  • Great! Glad you figured it out 🙂

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