Query_vars support in Rest API
Question
I am trying to make query_vars work in rest api. In my code bellow if I call wp-json/wp/v2/cpt?city=test
, my debug output always returns an empty string.
<?php
/**
* Plugin Name: Test
* Author: Gecka <contact@gecka.nc>
* License: GNU General Public License v3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
function cpt_init() {
$args = [
'label' => 'Custom post',
'supports' => [ 'title', 'revisions', 'custom-fields' ],
'show_in_rest' => true,
'hierachical' => false,
'public' => true,
'exclude_from_search' => true,
'show_ui' => true,
];
register_post_type( 'cpt', $args );
}
add_action( 'init', 'cpt_init' );
function cpt_register_query_vars( $vars ) {
$vars[] = 'city';
return $vars;
}
add_filter( 'query_vars', 'cpt_register_query_vars' );
function cpt_posts_where( $where, WP_Query $wp_query ) {
if('cpt' !== $wp_query->get('post_type') ) {
return $where;
}
// debug output
var_export( $wp_query->get('city') );
return $where;
}
add_filter( 'posts_where', 'cpt_posts_where', null, 2 );
0
2 years
2020-12-21T22:10:41-05:00
2020-12-21T22:10:41-05:00 0 Answers
4 views
0
Leave an answer