mompracem
Forum Replies Created
-
ok I did
but eventually I managed to achieve to do what I wanted by conditionally adding different filters depending on where the query is happening (front page, a post type archive, a singular post…)
what I wanted is to produce different api results based on the WordPress view. For example, if the user was querying the front page, I wanted a very compact json object.
so I added my filters a bit like this:
if ( is_front_page() ) { add_filter( 'json_prepare_post', 'my_front_page_filter', 10, 3 ); } else { add_filter( 'json_prepare_post', 'my_json_filter', 10, 3 ); }
so far this is the only way I found to achieve what I wanted…
it’s probably not the correct way, but I didn’t understand how to do that by using $context or within being in the same filter function
Hi Ryan, thank you…
but how do I access $context from ‘json_prepare_post’ filter? I tried, doesn’t look like it’s passed in there, php throws an error – what values $context can have?
thanks again
Hi Dunar,
yes I tried alread json_prepare_post… it would suffice if it were not for the fact that I can’t use conditionals such as is_singular, if this were a normal wp query… For example, for the archives, I don’t need custom metas or a lot of stuff, content, etc. If I could create a filter with something as:
if ( !is_singular() ) { unset([$_post]['content']); }
then I’d have what I want – I’m looking for something similar. It doesn’t work because each post in the object is looped individually, so it’s always singular – conditionals such as if ( get_post_type == ‘attachment’ ) will work and I can filter content of the json object according to post type for example, or other individual post parameters… What I need to do is alter the json object according to route… individual post or root /posts/ – I don’t need anything else ??
thanks
no, not quite
I need raw data, no html; because I use that data also in non-wp application and – later on – on external websites/mobileactually I’m fine with the API, but would like if I could format the json object in different ways, also to make it lighter
sorry for the late reply and many thanks for your help
I managed to filter $_post[‘post_meta’] directly inside json_prepare_post
reason I had to do this was to completely rewrite my [‘post_meta’] data
I’m using Advanced Custom Fields and arrays are natively stored in strings, whereas I needed arrays in the json object
what I’m doing now is to customise my json object according to my views and serve my wordpress frontend entirely in javascript – sometimes thought there’s a lot of unneccessary stuff in the json object and I want to get rid of unused parts, maybe conditionally (depending on the view I am)
do you have any hints regarding this? using common WordPress conditionals for query (is_singular, is_archive…) is useless, since the posts looped in the /posts/ route are all individual… I should filter the /posts/ route or should I create an entirely new route?
I’m a bit confused since I’m new in working with APIs and WP-API…thanks!
my json_prepare_meta (not! json_prepare_post) filter has conditionals like
if ( ‘something’ == get_post_type( $id ) {
// do something…
}or I need the id to format fields using advanced custom fields function get_field()… but get_field again requires an ID (without ID it will look in main query context, not wp-json)
i think reason why my code above doesn’t work is that prepare_meta in L578 is first filtered by json_prepare_meta… so if I append an ID or whatever there is passed after the filter is executed… but I need the ID before that…
I’m trying to use ‘json_prepare_meta’ which is where the post custom fields are printed in the json object
yes, that filter refers to a function that is instantiated inside prepare_post function… here:
https://github.com/WP-API/WP-API/blob/master/lib/class-wp-json-posts.php#L578
I tried to do the following to append the ID to $_post[‘post_meta’] by filtering ‘json_prepare_post’ (pretend it’s a custom field to retrieve in ‘json_prepare_meta’), but it does not work:
$id = array( 'ID' => $_post['ID'] ); $_post['post_meta'] = array_merge( $_post['post_meta'], $id ); return $_post;
I think doesn’t work because it
how can I pass the ID inside ‘json_prepare_meta?
here: https://github.com/WP-API/WP-API/blob/master/lib/class-wp-json-posts.php#L618 I can only access $custom_fields, which is an array of custom fields…I gave up because there’s no way to access $post_id while in prepare_meta… there’s a way to access it only in prepare_post function
I suggested the WP-API team to make it accessible there too, at least the ID
no it seems I can’t still get it…
I’m looking to get $post_id variable or something equivalent to get_the_ID()
I see $post_id everywhere in wp-api but I don’t get where it comes from…
there’s something in WP_JSON_Posts class with public function getPosts… this seems to be used to get the post object(s) and therefore the ID as well, will give a try
thanks but doesn’t look like to be there (in the WP_JSON_Server class) –
I’m still searching and trying to figure itI need to use this when I’m in a collection of posts (ie archive), therefore there could be many IDs in a paged json document
Forum: Plugins
In reply to: [Secure Custom Fields] Option table is filled up with ACF dataPerhaps you should ask the ACF support forums https://support.advancedcustomfields.com/