0

I'd like to filter by Custom Fields, my goal is to have links with url parameters like:

domain.com/?custom_field=white

I think i can create the custom query by tweaking this examples: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

But how can i put all together in a site plugin or functions.php so when someone clicks my URL parameter it actually runs my query? I can't seem to find the answer to that step.

1 Answer 1

0

What you want to do is access the query_vars and use them.

Base code:

Page to access the fields

$custom_field = get_query_var('custom_field');
var_dump($custom_field);

And the function or custom plugin to save the magic:

add_filter( 'query_vars', 'my_custom_query_vars' );
function my_custom_query_vars( $vars ) {
    $vars[] = 'custom_field';
    $vars[] = 'custom_awesome_field';

    return $vars;
}

yourawesomesite.awesome/?custom_field=white

string 'white' (length=5)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.