0

I am using wooCommerce. How to display posts, when seaching by custom field value. The goal is when ?s={custom_field_value} URL is set, to get a result : ?custom_field*={custom_field_value} And get a list of products which has that custom_field_value in custom_field.

*?custom_field is the name of custom_field.

2
  • Just note, s is a reserved query var which is used for search queries. Commented Jul 1, 2016 at 6:57
  • I've updated my answer in line with your edit.
    – Tim Malone
    Commented Jul 4, 2016 at 21:33

1 Answer 1

1

You can't do this by default, but you can write a query to do it within your theme. It could be a security risk to allow any field to be entered in the URL, so you'll need to explicitly detect those that you are wanting.

In your functions.php, you could add something like this (untested!):

add_action( 'pre_get_posts', 'wpse230535_custom_posts_by_custom_field' );

function wpse230535_custom_posts_by_custom_field( $query ) {
  if( !is_admin() && $query->is_main_query() ) {

    $acceptable_fields = array( 'custom-field-1', 'custom-field-2' );
    $meta_query = array();

    foreach( $acceptable_fields as $field ) {
      if ( ! isset( $_GET[$field] ) ) { continue; }

      $meta_query[] = array (
        'key' => $field,
        'value' => sanitize_text_field( $_GET[$field] ),
        'compare' => '=',
      );

    } // end foreach

    if( count( $meta_query ) ) { $query->set( 'meta_query', $meta_query ); }

  } // end if
} // end function

This will take your allowed custom fields - currently custom-field-1 and custom-field-2, check whether they're set in the query string (via the $_GET[] superglobal), and then if so, sanitize them and add them to a meta query.

You'll need to replace the allowed field names with the ones you want, of course. As an example, this code would, when one visits http://www.example.com/?custom-field-1=fieldvalue, only show posts that have a custom field called custom-field-1, with the exact value of fieldvalue. You can add as many fields as you want.

For further reference on making queries by custom fields, you can see the WP Meta Query documentation. Hope this helps!

3
  • No, you can't retrieve a post, by custom field, directly by URL Not true. You can pass anything through the URL and let that alter the main query accordingly. All you need is to check for your custom $_GET variable in the URL, and then use pre_get_posts to alter the main query accordingly Commented Jul 1, 2016 at 6:20
  • @PieterGoosen True, I didn't elaborate on that though because I didn't know if that's what the OP wanted - it was unclear if they wanted to do a dynamic query as such or just a static query.
    – Tim Malone
    Commented Jul 1, 2016 at 6:30
  • @PieterGoosen Post updated in line with OP's edit.
    – Tim Malone
    Commented Jul 4, 2016 at 21:32

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.