Get Custom Fields from wp-json response - wordpress

The response for all posts in wp-json does not include a couple of Custom Fields that I created in my posts. I call it this way: /wp-json/posts
How can I make sure the JSON response also contains my Post Custom Fields?

Check out this link.
Turns out this is a problem at the mo in Wordpress, but the link has a suggestion fix (amongs others) as such :
function json_api_prepare_post( $post_response, $post, $context ) {
$field = get_field( "field_name", $post['ID'] );
$post_response['field_name'] = $field;
return $post_response;
}
add_filter( 'json_prepare_post', 'json_api_prepare_post', 10, 3 );
You will however have to go through the full post as the above link, s it turns out a lot of people are having this issue, but a number of them got the issue resolved with all the suggestions in the post.
It also appears this question has been asked before. refer to this question.

Related

Ordering By Menu Order in the Wordpress REST API?

I am trying to display posts using the REST API and I want to display them in menu order based on how they are positioned in the admin menu.
I installed a plugin that allowed me to move posts to any position in the admin. I know querying the posts the normal way with wordpress there is a orderby: menu_order option, which would do what I am looking for, but I can't figure it out with the REST API.
My REST API looks like this:
https://example.com/wp-json/wp/v2/qd_leadership?_embed&per_page=100&orderby=menu_order
So I've tried that and that does not work. It says menu_order isn't an option. I also saw a post here:
Query WordPress (REST) posts by the order they appear on the admin
That had a similar question. The only answer on that post is to not have any orderby parameter and that should display them in the menu order, but that did not work for me. So I am stumped on how to order the posts from the REST API in menu order?
Its bug of wp core in rest api so you could use below hack for the solution.Please add below code in your active theme's function.php
add_filter( 'rest_post_collection_params', 'my_prefix_add_rest_orderby_params', 10, 1 );
function my_prefix_add_rest_orderby_params( $params ) {
$params['orderby']['enum'][] = 'menu_order';
return $params;
}
Tested and works.
Thanks to raju_eww for the hint in the right direction. But in case of a custom post type collection the filter hook name has to be like this:
add_filter( 'rest_custom-post-type_collection_params', 'my_prefix_add_rest_orderby_params', 10, 1 );
function my_prefix_add_rest_orderby_params( $params ) {
$params['orderby']['enum'][] = 'menu_order';
return $params;
}
found here:
https://www.timrosswebdevelopment.com/wordpress-rest-api-post-order/
Following code do sort by menu_order when orderby is not in query string (for woocommerce):
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
if(!$_GET['orderby']) {
$args['orderby'] = 'menu_order';
$args['order'] = 'asc';
}
return $args;
}
source

Custom Columns Don't Display Anything

I have added a new column to a custom post type to display the post's ID. This works for the WordPress core post type, but not for my custom post type. I have tried using the manage_{post_type}_custom_column hook and just applying it to all posts, but neither works.
It DOES add the custom column headers, but I can't populate them with anything at all when viewing the custom post type.
This is what it looks like when viewing the custom post type
and
this is what it looks like when viewing a regular core post.
// Add post ID column to use an order ID in all posts view.
add_filter('manage_posts_columns', 'oms_order_id_header');
add_action('manage_posts_custom_column', 'oms_order_id_column', 10, 2);
function oms_order_id_header($columns) {
//Remove title column
//unset($columns['title']);
//Add new columns
$columns['order_id_header'] = 'Order ID';
$columns['customer_header'] = 'Customer';
$columns['author'] = 'Owner';
return $columns;
}
function oms_order_id_column( $column, $post_id ) {
if ($column == 'order_id_header') {
echo $post_id;
}
}
This turned out to be an issue with the post type being set to hierarchical. Hierarchical post types need to be targeted by a different action hook than the one used here.
Instead of manage_posts_custom_column, hierarchical post types need to use manage_pages_custom_column.
I just tried you code and it seems to be working perfect on my WordPress installation in both cases: Custom Post and Post.
Maybe your error is happening because your posts are drafts? I don't think so but maybe. (I tried your code also with drafts on my installation and it worked). Here is the screenshot:
Try printing "hello world" instead of the $post_id to check if it prints in all cases.

Wordpress: Job Manager: How to modify Resume custom fields?

I have been trying to figure out a filter or action to modify Resume custom fields.
There is a documentation here about how to do it for resume core fields but not for custom fields.
If I use submit_resume_form_fields filter like
add_filter( 'submit_resume_form_fields', 'remove_submit_resume_form_fields' );
function remove_submit_resume_form_fields( $fields ) {
$fields only returns resume core fields but not the custom fields.
Can anyone help me?
Finally I got it worked. So instead of using submit_resume_form_fields filter, I used submit_resume_form_fields_get_resume_data and it gave me all the fields (with custom fields as well). Unfortunately I could not find a documentation of this, I had to search the code. So the code goes like
add_filter( 'submit_resume_form_fields_get_resume_data', 'remove_submit_resume_form_fields' );
function remove_submit_resume_form_fields( $fields ) {
unset( $fields['resume_fields']['custom_field_name'] );
}
I thought it might help someone!

WooCommerce Url rewrites

I've got the same problem described in this post:
[WordPress URL rewrite for WooCommerce attributes, except that I need to filter by attribute not only inside a category.
Unfortunately, I cannot post a comment before reaching a higher reputation, so I'm creating a new question.
I defined a manufacturer attribute and if I want to browse all products from a certain manufacturer, I can use a url like www.example.com/shop/?filter_manufacturer=230, where 230 is the attribute ID.
I tried adding a endpoint, like suggested in the post linked above above, but I cannot get the rewrites working; for example, if I try to open www.example.com/shop/manufacturer/manufacturer_name I get a 404 error.
It's not clear to me if I should change anything in the permalink settings in Wordpress and, if yes, how.
I've always flushed the rewrite rules after every edit, BTW.
The missing link between your question regarding WooCommerce attributes and the linked answer is that product attributes are merely taxonomies with a 'pa_' appended to their name.
In your case the taxonomy is called "pa_manufacturer". WooCommerce sets these up by default to have no query var attached.
So in lieu of filtering query_vars we are going to target when WooCommerce registers that particular taxonomy. I've also modified to remove anonymous functions.
In my example I am using "color", so adjust to "manufacturer". I was able to then go to a URL of http://example.com/shop/color/black and see all the black products. Note that this doesn't get you a term archive where /shop/color will list all the colors. That is a different question and a lot more work.
I didn't test the activation part, so if you get 404s after activating you can just delete the whole activation function and simply go to Settings>Permalinks and save the permalinks again.
/**
* Plugin Name: Add an WooCommerce attribute endpoint to the URLs
* Plugin URI: http://stackoverflow.com/q/28460538/383847
* Credit to: http://stackoverflow.com/a/24331768/1287812
*/
function so_28460538_add_rewrite_endpoint(){
add_rewrite_endpoint( 'color', EP_ALL );
}
add_action( 'init', 'so_28460538_add_rewrite_endpoint' );
function so_28460538_attribute_args( $args ){
$args['query_var'] = 'color';
return $args;
}
add_filter( 'woocommerce_taxonomy_args_pa_color', 'so_28460538_attribute_args' );
/**
* Refresh permalinks on plugin activation
* Source: http://wordpress.stackexchange.com/a/108517/12615
*/
function WCM_Setup_Demo_on_activation(){
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
add_rewrite_endpoint( 'color', EP_ALL ); #source: http://wordpress.stackexchange.com/a/118694/12615
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'WCM_Setup_Demo_on_activation' );
EDIT
Adding some screenshots of my settings in case it will help determine why you are getting 404s:
Here are my permalinks settings:
and here is the WooCommerce setting for determining the product archive page:
And finally, here is the result of visting:
http://local.wordpress.dev/shop/color/black/
Where shop is the pretty permalink for the product archive page set above. All items have a 'black' color attribute.
I had a similar problem and was able to solve it like this:
function add_model_taxonomy_args($args) {
$args['query_var'] = 'filter_model';
return $args;
}
add_filter('woocommerce_taxonomy_args_pa_model', 'add_model_taxonomy_args' );
function custom_rewrite_rules() {
add_rewrite_tag('%filter_model%', '([a-zA-Z0-9-]+)');
add_rewrite_rule('^c/phone-cases/(.+?)/?$', 'index.php?product_cat=phone-cases&filter_model=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_rules', 10, 2);
Sample Url: c/phone-cases/iphone-8-plus/
Resulting Rewrite: index.php?product_cat=phone-cases&filter_model=iphone-8-plus

What is difference between these Wordpress filter hooks

I'm new to WordPress plugin development. I have a doubt about below 3
filter hooks.
content_edit_pre
content_filtered_edit_pre
excerpt_edit_pre
Please tell me the what the difference between these hooks.
content_edit_pre filter hook
The content_edit_pre filter hook is used to hook into the content of a post just before it is loaded to be edited. For example, if you included the following at the end of your functions file:
function test_of_content_edit_pre( $content, $post_id ) {
return "Insert this before the content about to be edited ".$content;
}
add_filter( 'content_edit_pre', 'test_of_content_edit_pre', 10, 2 );
Then open a post to edit it, you would see that the text has been inserted before the post:
excerpt_edit_pre filter hook
The excerpt_edit_pre filter hook is very similar to content_edit_pre, except it is used to hook into excerpts (instead of posts) just before they are loaded to be edited. For example:
function test_of_excerpt_edit_pre( $content, $post_id ) {
return "Add this to excerpt".$content;
}
add_filter( 'excerpt_edit_pre', 'test_of_excerpt_edit_pre', 11, 2 );
Would result in this being shown in the excerpt:
content_filtered_edit_pre filter hook
This one I am not sure about. I tested it out and it didn't seem to do anything. I will update my answer if I can find more information on this.

Resources