Create filtered list of posts using JSON data

First, you must allow your custom field be queried from the REST API. Let’s start simple with an example from 1fix.io: function my_allow_meta_query( $valid_vars ) { $valid_vars = array_merge( $valid_vars, array( ‘meta_key’, ‘meta_value’ ) ); return $valid_vars; } add_filter( ‘rest_query_vars’, ‘my_allow_meta_query’ ); So, asuming your field name is called “favorite_animal”, for this example, you can … Read more

Importing JSON feed should the content be sanitized?

There are two aspects here obviously all input should be sanitized JSON is just a wrapper no different then any other type of container which is used to aggregate data for transmission. You almost never sanitize the container as usually in case of an error you will just not be able to extract the data … Read more

How to use the new WordPress 4.4 JSON API?

The WP REST API is being developed for WordPress as a Feature Plugin. A Feature Plugin is: … the way for features to be developed for inclusion in WordPress core. This model allows a feature to be built, tested, refined, and polished before it is considered as a merge candidate source: https://make.wordpress.org/core/handbook/about/release-cycle/features-as-plugins/ Therefore, the WP … Read more

How to retrieve wp_ json_encode data from custom WordPress database table

$wpdb->get_results() returns an Array of rows representing the results. The rows themselves can be represented as objects or arrays depending on the second argument. See the documentation: output_type One of four pre-defined constants. Defaults to OBJECT. See SELECT a Row and its examples for more information. OBJECT – result will be output as a numerically … Read more

Unset data in custom post type WordPress API (wp-json)

If possible, only the examples shown in internet is: function qod_remove_extra_data($data, $post, $context) { // We only want to modify the ‘view’ context, for reading posts if ($context !== ‘view’ || is_wp_error($data)) { return $data; } // Here, we unset any data we do not want to see on the front end: unset($data[‘author’]); unset($data[‘status’]); // … Read more

Include custom post meta value in fetched JSON

Two things to look at: 1) register_meta() 2) custom-fields support on post type. 1) register_meta You can register your custom post meta with show_in_rest => true to make it accessible via the api. Bottom of this page: Modifying Responses, provides the following example: <?php // The object type. For custom post types, this is ‘post’; … Read more

Import JSON feed to WordPress

json_decode the JSON into an array. $slices = json_decode(file_get_contents(‘yourJSONFile.json’),true); Loop into the data if ($slices) { foreach ($slices as $slice) { $title = $slice[1]; // insert more logic here } } Create a post programmatically by using wp_insert_post. // Create post object $my_post = array( ‘post_title’ => $title, ‘post_content’ => ‘This is my content’, ‘post_status’ … Read more

What is /wp-json?

That’s the root URL for the REST API. All WordPress installs have it, but in 4.6 very few endpoints exist, mostly oembed and plugins. The core infrastructure for the REST API has been available since 4.5, with functions such as register_rest_route being available. /wp-json itself is generating discovery data, listing the various available endpoints. You … Read more

WP API returning SQL results as strings, rather than numbers

The string output type is expected for $wpdb query results, the db data types are not mapped to the corresponding PHP data types. You will have to take care of it yourself, like: $data = [ ‘int’ => (int) ‘123’, ‘bool’ => (bool) ‘1’, ‘string’ => (string) ‘abc’ ]; return rest_ensure_response( $data ); with the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)