Is there any way that I can get today's top post's IDs from google analytic or from Parsely API.
I want to show it under "Todays Most Viewed" section of my news website. It shows top 5, posts which where viewed most.
Thank you in advance.
I found a way using parsely:
/*
Function to return latest post, which are retrived from Parse.ly's API
*/
public static function get_most_viewed_homepage_articles ($limit=6) {
$attr= array('apikey' => "YOUR_API_KEY",
"secret" => "YOUR_API_SECRET",
"page" => "1",
"limit" => "$limit",
"time" =>"5m");
$url="https://api.parsely.com/v2/realtime/posts";
$url.="?";
reset($attr);
while(list($attr_key, $attr_val) = each($attr)){$url.=$attr_key."=".$attr_val."&";}
$result = #file_get_contents($url);
$result_json = json_decode($result, true);
return $result_json;
}
This worked for me. I used it on function.php
Related
I want to be able to add meta to a media post type by using WP REST API.
I want to use Postman because, for now, I just want to test how the API is working. The docs seems to be somewhat confusing. I would be grateful if you have any working examples.
Basically, I want to add copyright meta field to the media using this API.
for creating API you need to add route first. you can add route using below code:
function custom_meta_api() {
register_rest_route('wp/v1', '/update_meta/(?P<id>[\d]+)', array(
array(
'methods' => 'POST',
'callback' => 'saveMeta',
),
));
}|
add_action('rest_api_init', 'custom_meta_api');
you can pass your image id in (?P<id>[\d]+)
now in postman write url
http://your-url/wp-json/wp/v1/update_meta/5 with POST request
in body you can write below code
{"data":
{
"copyright":"xyz"
}
}
and to save in postmeta table create function saveMeta(which you have written in callback). Code for the function is below:
function saveMeta(WP_REST_Request $data) {
$bookingID = $data['id'];
$request = $data->get_json_params();
extract($request['data']);
update_post_meta($bookingID, 'copyright', $copyright);
$response = array();
$response["code"] = "success";
$response["message"] = "";
$response["data"] = array();
$response["data"][] = 'meta added';
return $response;
}
is it possible to get post data/type from the wordpress rest api using url as parameter?
I knowing this function already from soundcloud. There is an resolve function with an url parameter (see https://developers.soundcloud.com/docs/api/reference#resolve). When you call this rest api endpoint, you get detailed information if the url is for example a track or playlist.
Unfortunately i couldn't find a function at the wordpress rest api documentation for doing this. What i exactly want is to get the post data/type by only knowing the url
https://demo.wp-api.org/2017/05/23/hello-world/ => post
https://demo.wp-api.org/example => page
https://demo.wp-api.org/category/example => category
With a function like this
https://demo.wp-api.org/wp-json/wp/v2/resolve?url=https://demo.wp-api.org/2017/05/23/hello-world/
Is there a way to do this? Or do i need to write my own rest api endpoint with a wordpress plugin?
this solved the problem
function route_handler($request)
{
global $wp;
global $wp_query;
$parameters = $request->get_json_params();
$url = $parameters["url"];
$_SERVER['REQUEST_URI'] = $url;
$wp->parse_request();
$wp->query_posts();
//Insert queries for more specific information
//Modify or simplify the query results
//Here I'm just returning the query results.
return json_encode($wp_query);
}
add_action('rest_api_init', function () {
register_rest_route('custom-theme/v1', '/route', array(
'methods' => 'POST',
'callback' => 'route_handler',
));
});
see: https://medium.com/#harryhorton/query-wp-rest-api-using-any-permalink-url-f5e4e4dd36b7
You could just get all the posts https://demo.wp-api.org/wp-json/wp/v2/posts and then do some thing like:
$.each(data, function(index, post){
if(post.link == yourUrl){
//yourcode
}
});
If you don't want to write your own endpoint
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'm using Wordpress as the backend to my website where people can create accounts and write posts. Posts should only be viewable by the author of the post. I've handled this in most places on the site, for example, the query for the list of posts:
$wpb_all_query = new wp_query(array('author' => $current_user->ID, 'post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1));`
I'm now realizing that people can see posts by other authors if they manually enter the URL of the post (which would be easy to guess). How can I redirect people if they are not the author of a post they are trying to view?
Wordpress has provide core function for check that is user is auther or not..
$user = wp_get_current_user();
if (!is_author()) {
//The user has the "author" role
}
I hope its helpfull to you
All you need to do is ,
$required_capability = 'edit_others_posts';
$redirect_to = '';
function redirect_user() {
global $required_capability, $redirect_to;
if (!current_user_can($required_capability)) {
if ($redirect_to == '') { $redirect_to = get_option('home');}
wp_redirect($redirect_to,302);
}
}
add_action('init','redirect_user',0);
Change the capability to read view edit , etc.. whatever you want to apply for the current user.