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!