I want to add a custom field to be included into my search query. That's done via meta_query, I am fully aware of that but the issue is, I don't know, where to hang into to manipulate the query args to my needs.
So I am looking for a filter, to to get into REST API search request (/wp/v2/search), any clue?
You can implement the following hook:
add_filter( 'rest_post_search_query', 'rest_search_add_custom_field_cb', 10, 2 );
function rest_search_add_custom_field_cb( $query_args, $request ) {
// filter...
return $query_args;
}
You can modify the search query and include additional params like meta query, etc.
The return value of this filter will be used by WordPress in the following context:
...
$query->query( $query_args );
...
Related
I want to create a nice readable permalink structure for my custom post type (CPT). My CPT "movie" has the following rewrite-slug movie/movie_name" (all works fine).
Now i want to add arg like this: movie/movie_name/arg and use arg in my template file as a php variable.
But obvious it lead to not-found-page. How can i achieve this target?
edit: i want it in FRIENDLY URL format, it means i dont want to use GET for this.
You may pass it like movie/movie_name?movie_arg=movie_value. It is will be available with $_GET['movie_arg']. Of course your need extra sanitization to handle this data.
To be able to read this in a WordPress way add params to a query_vars filter
function add_movie_arg_to_query_vars( $qvars ) {
$qvars[] = 'movie_arg';
return $qvars;
}
add_filter( 'query_vars', 'add_movie_arg_to_query_vars' );
Note: it should not be same as reserved WordPress query parameters
This way it will be available at your template with get_query_var('movie_arg')
print_r( get_query_var('movie_arg') ) // movie_value
More information here
I'd appreciate any advice, resources or assistance with this issue.
I want to be able to have part of my Wordpress site where I can parse the URL and then use those values to populate the page with content from another API.
For example:
server.zz/weather/Sydney%20Australia
server.zz/weather/Houston%20Texas
Where I could write a Plugin which would intercept these requests, be able to extract the end of the URLs, and then call another API to get the data to then merge into a Template to be presented to the visitor.
I know that there are Custom Post Types, but I wasn't sure if they were the best solution for this usage case.
As I said, any advice or suggestions would be appreciated.
I found the solution to this problem by using add_rewrite_rule(), add_rewrite_endpoint(), and flush_rewrite_rule().
For the example I provided earlier, I created the following code in a Plugin.
// Define the URL Rewrite Rules
function crw_rewrite_urls(){
add_rewrite_rule(
'^weather/(.+)$' ,
'index.php?weather_location=$matches[1]' ,
'top'
);
add_rewrite_endpoint('weather_location', EP_ROOT);
flush_rewrite_rules();
}
add_action('init', 'crw_rewrite_urls');
// Initialise the Query Variable
function crw_query_var( $vars ) {
$vars[] = 'weather_location';
return $vars;
}
// Check for the Variable and Display Content as needed
function crw_handler() {
global $wp_query;
if ( isset( $wp_query->query_vars['weather_location'] ) ) {
// Call the API, fill the Template here
}
return;
}
add_action('template_redirect', 'crw_handler');
I currently have this a meta fields that are registered to the post, and another post type.
I was able to register the metafields correctly and I am able to do a POST for me articles through REST API.
What I am trying to figure out now is how can I add the meta key fields as search keys when I try to do a GET request through the rest API.
I searched through google but I am getting outdated resources.
I currently have this code:
$this->post_types = array ( 'post', 'events' );
foreach( $this->post_types as $field ) {
add_filter("rest_" . $field . "_query", function ($args, $query) {
$args["meta_query"] = "event_id";
$args["meta_query"] = "event_date";
return $args;
}, 10, 2);
}
But when I try to search using: /wp-json/wp/v2/events?filter[meta_query][event_id]=15432 I am getting incorrect results.
Is there something in my code that I missed? Thanks in advance for any help.
So I'm trying to work with the WP REST API. Using latest version of WP. I am using this in an external application and testing with Postman.
This is what I want to do:
display custom meta fields in the GET posts request
GET all posts (no limit)
create / update / delete (multiple) meta fields in one API request
Are these things possible with WP REST API? If so, can anyone share some examples?
I know all these work very well with WooCommerce REST API.
The better way is to use a custom endpoint to achieve this. You must create your own plugin for this...
//register different functions for different methods, use parameters in url for GET calls
register_rest_route('plugin_name', 'your_endpoint', array(
array('methods' => 'POST',
'callback' => 'magic_function',
))
);
function magic_function( $request ) {
//You can filter the query to get all posts (rest_{$this->post_type}_query)
add_filter('rest_post_query','my_custom_query', 10, 3);
$custom_request = new WP_REST_Request( 'GET', '/wp/v2/post');
$response= rest_do_request( $custom_request );
$response->data['meta_field'] = get_post_meta($response->data['id'], 'meta_field',true);
return new WP_REST_Response($response->data);
}
function my_custom_query($args, $request){
//returns all posts in request...
$args['numberposts'] = -1;
return $args;
}
I am trying to combine the search by keyword and meta_key and meta_value filter in WP_query. Keyword search and meta filter works good independently, but together are not. How to get it done? Thanks!
Here is my code:
if ( isset($_REQUEST['search']) AND $_REQUEST['search'] )
{
$query_param = array( 's' => urldecode($_REQUEST['search']), 'post_type' => GOODS_POST_TYPE );
$query_param['meta_key'] = 'sku';
$query_param['meta_value'] = urldecode($_REQUEST['search']);
}
$query = new WP_Query($query_param);
Modify your query before the search is run, and use Wordpress' built-in functions to do what you need rather than relying on unfiltered data:
<?php
add_action('pre_get_posts', 'modify_goods_search');
function modify_goods_search($query)
{
if($query->is_search)
{
$query->set('meta_key', 'sku');
$query->set('meta_value', get_search_query());
$query->set('post_type', GOODS_POST_TYPE);
}
}
?>
Remember that this will modify ALL searches. If you want to modify only one particular search, you can modify the conditional to make sure you're on a particular page before modifying the given query.
You might also want to consider using meta_query rather than just meta_key and meta_value.
Information on everything:
Pre Get Posts
Custom Field Parameters in Meta Query
Get Search Query