Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to sort/order by custom metabox fields #39

Open
b-lov opened this issue Mar 31, 2022 · 3 comments
Open

allow to sort/order by custom metabox fields #39

b-lov opened this issue Mar 31, 2022 · 3 comments

Comments

@b-lov
Copy link

b-lov commented Mar 31, 2022

hey, thanks for the awesome plugin!

would it be possible to allow graphql to sort / order by custom fields?

thanks in advance

Screenshot 2022-03-31 at 11 50 33

@b-lov b-lov changed the title allow to filter by custom fields allow to sort/order by custom metabox fields Apr 2, 2022
@hsimah
Copy link
Member

hsimah commented Apr 9, 2022

I'm sure it's possible. Let me take a look. It's been a while since I worked on this so hopefully we're not too out of date.

@b-lov
Copy link
Author

b-lov commented Apr 11, 2022

hey man, thanks for the reply. i since figured out how to make a custom field orderable, but since i don't really have any php experience i didnt yet have time to figure out how to do it programmatically. anyway here is the code i use to make one field orderable:

$field_id = 'my_custom_field'

// register field for ordering
add_filter('graphql_PostObjectsConnectionOrderbyEnum_values',
    function ($values) use ($field_id) {
        $values[strtoupper($field_id)] = [
            'value' => $field_id,
            'description' => __('my custom description', 'wp-graphql'),
        ];
        return $values;
    }
);

// make orderable
add_filter('graphql_post_object_connection_query_args',
    function ($query_args, $source, $input) use ($field_id) {
        $orderby_inputs = $input['where']['orderby'];
        if (isset($orderby_inputs) && is_array($orderby_inputs)) {
            foreach ($orderby_inputs as $orderby) {
                if (!isset($orderby['field']) || $field_id !== $orderby['field']) {
                    continue;
                }
                $query_args['meta_key'] = $field_id;
                $query_args['orderby'] = 'meta_value meta_value_num';
                $query_args['order'] = $orderby['order'];
            }
        }
        return $query_args;
    }, 10, 3
);

@hsimah
Copy link
Member

hsimah commented Apr 25, 2022

That is super helpful. I am quite good with PHP these days, so I can use that to add to the plugin. I am moving homes at the moment, but I hope to take a look soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants