can’t get url parameters working with metadata for searching posts
Question
I am trying to get a custom feild added as a search paramter url.
my post metadata for this example is ‘public_paywall’ and want to register as it as a query var “access”
This is the code I have tried to no avail:
/////////////register var//////////////
function myplugin_register_query_vars( $vars ) {
$vars[] = 'access';
return $vars;
}
add_filter( 'query_vars', 'myplugin_register_query_vars' );
////////////add ///////////////////
function myplugin_pre_get_posts( $query ) {
// check if the user is requesting an admin page
// or current query is not the main query
if ( is_admin() || ! $query->is_main_query() ){
return;
}
// edit the query only when post type is 'food'
// if it isn't, return
if ( !is_post_type_archive( 'post' ) ){
return;
}
$meta_query = array();
// add meta_query elements
if( !empty( get_query_var( 'access' ) ) ){
$meta_query[] = array( 'key' => 'public_paywall', 'value' => get_query_var( 'access' ), 'compare' => 'LIKE' );
}
if( count( $meta_query ) > 1 ){
$meta_query['relation'] = 'AND';
}
if( count( $meta_query ) > 0 ){
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'myplugin_pre_get_posts', 1 );
I have posts with the value ‘paywall’ for the key ‘public_paywall’but my search urls dont work:
http://localhost/vraudiogigscom/?post_type=post&access=paywall
what am I doing wrong? I have been searching other examples and not getting anywhere
cheers
0
custom-field, meta-query, parameter, post-meta, search
5 months
0 Answers
57 views
0
Leave an answer
You must login or register to add a new answer .