I use plugins: ACF and ACF to REST API.
I created group of custom fields, turned on it to show and edit in rest. And setup it to show only post type - post. Ok. I got acf object with my custom fields in rest api as /wp-json/wp/post/1 and /wp-json/acf/v3/posts/1.
But I want to get these properties in only url /wp-json/acf/v3/posts/1, not /wp-json/wp/post/1. In other words, these properties mustn't be in Post Api object.
How can I do it properly? Thanks.
Thanks for using my plugin.
Please copy and paste the filter below in your functions.php.
add_filter( 'acf/rest_api/post/get_fields', function( $item, $request ) {
if ( is_array( $request ) ) {
$item = array();
}
return $item;
}, 10, 2 );
Thanks
Related
I have a Custom Post type with two custom fields - Description and Attachment (to upload file/PDF).
When I complete this Custom Post I want the link to go directly to the attachment rather than the post page. I am using CPT UI and Custom Fields plugins to manage all this.
Does anyone know how I can create a custom post that will go directly to an attachment rather than the post page? I want to be able to display the title of each post on a page and have the title go to the attachment within the post.
I hope this makes sense and any help greatly appreciated!
This example assumes that you are using ACF to create the fields. And the field with the file gives its ID when requested (when creating a field in ACF there is an option to give ID or link)
add_filter( 'post_type_link', 'custom_post_permalink1', 10, 4 );
function custom_post_permalink1( $permalink, $post, $leavename, $sample ) {
// Change here to your post type name
if ( $post->post_type == 'your_post_type' ) {
$post_current_id = $post->ID;
// Change here 'file' to your custom field slug
if(get_post_meta($post_current_id, "file", true) ):
$PDF_ID = get_post_meta($post_current_id, "file", true);
$PDF_URL = wp_get_attachment_url( $PDF_ID );
$permalink = $PDF_URL;
endif;
}
return $permalink;
}
I have a custom post type named houses. Inside my custom post type I have several custom fields which I created using ACF.
What I need to do is to change the permalink when I create a new post.
I would like to use the code and title fields to customize the permalink:
//code + post title
4563312-house-example-1
I'm developing a plugin which controls everything.
Is there a way to intermediate the creation of a post to update its permalink?
Thanks.
After some research, I found an answer related to wp_insert_post_data.
Using wp_insert_post_data, I couldn't get the custom field value, and to achieve that, I had to use another action, save_post instead.
function rci_custom_permalink($post_id) {
$post = get_post($post_id);
if($post->post_type !== 'houses') return;
$code = get_field('code', $post_id);
$post_name = sanitize_title($post->post_title);
$permalink = $code . '-' . $post_name;
// remove the action to not enter in a loop
remove_action('save_post', 'rci_custom_permalink');
// perform the update
wp_update_post(array('ID' => $post_id, 'post_name' => $permalink));
// add the action again
add_action('save_post', 'rci_custom_permalink');
}
add_action('save_post', 'rci_custom_permalink');
PS: Since all these fields are required, I didn't need to check if they are empty or not.
For reference about save_post action:
Plugin API/Action Reference/save post
I am trying to get categories of custom post type but unable to find any solution. The WP REST API documentation returns only the categories of blog post.
Custom posts API: https://themographics.com/wordpress/service-providers/wp-json/wp/v2/sp_categories
Categories API: https://themographics.com/wordpress/service-providers/wp-json/wp/v2/categories [returns default categories of blog post type]
Is there any way like
https://themographics.com/wordpress/service-providers/wp-json/wp/v2/sp_categories/categories
if someone still looking for a solution:
get all:- https://yoursite.com/wp-json/wp/v2/your-taxonomy
get single:- https://yoursite.com/wp-json/wp/v2/your-taxonomy/id
get single by slug:- https://yoursite.com/wp-json/wp/v2/your-taxonomy?slug=slugname
in my case I used
https://app.local/wp-json/wp/v2/works-categories
https://app.local/wp-json/wp/v2/works-categories/5
https://app.local/wp-json/wp/v2/works-categories?slug=my-slug-name
source: similar post
You can do it this way:
add_action('init', 'json_handler');
function json_handler(){
$categories = get_terms( 'my_cat', 'orderby=count&hide_empty=0' );
if( ! is_wp_error( $categories ) ) {
// encode the $categories array as json
print_r( json_encode( $categories ) );
}
}
I am using the Custom Post Type UI plugin for worpdress and have created custom "pages". I have set these pages to be able to use the page template dropdown but would like to know if anyone knows of a way to show separate page templates for different post types?
You will need the theme_page_templates filter hook to remove the templates that you do not want to see for each post type. When the templates are loaded, including for display in the edit screen the templates that are returned are first passed to this function:
apply_filters ( 'theme_page_templates', array $page_templates, WP_Theme $this, WP_Post|null $post )
To implement you will use something like the following code:
function filter_theme_page_templates( $templates, $theme, $post ){
// make sure we have a post so we know what to filter
if ( !empty( $post ) ){
// get the post type for the current post
$post_type = get_post_type( $post->ID );
// switch on the post type
switch( $post_type ){
case 'custom-post-type':
// remove anything we don't want shown for the custom post type
// array is keyed on the template filename
unset( $templates['page-template-filename.php'] );
break;
default:
// if there is no match it will return everything
break;
}
}
// return the (maybe) filtered array of templates
return $templates;
}
add_filter( 'theme_page_templates', 'filter_theme_page_templates', 10, 3 );
Here my problem.
I have several users that are allowed to publish content.
I have create a custom post type to be used has a personal page.
I create a new custom post for each of the user.
They can publish media, events, posts... on this custom post, using tools provided by the theme Flawless.
The Flawless tools publish by default all the content, so if you want to add a gallery, it will show all gallery by all users. I want to limit the gallery display to only the current author of the custom post. And the same for all type of content a user may want to add to his page.
My understanding was that pre_get_posts was perfect in this kind of situation.
The goal was to publish on their custom post only their content, so I used the action pre_get_posts with this function:
function only_current_author( $query ) {
if ( !is_admin() && $query->query_vars['post_type'] != 'nav_menu_item' ) {
$post_id = get_queried_object_id();
$author_id = get_post_field( 'post_author', $post_id );
$query->query_vars['author'] = "$author_id";
}
}
add_action( 'pre_get_posts', 'only_current_author' );
This was working fine on my localhost with WordPress 3.9
Now I have updated to WordPress 4.0, and I got an error 404.
I don't understand exactly what cause the issue, and what is different between the two WordPress version, but I think this is what caused the error 404 is that I'm not able to get the author id of the current custom post.
So, I wonder, do you know alternative to define the author id inside the pre_get_posts action ?
Or should I use another method to filter what will be display?
I try several alternative, like adding an extra Query or adding the $query->query_vars in the template in my custom template, but none worked.
I try to get the author id using the name of the custom post found in the $query, nothing seems to work.
Thanks for your help.
So, after a good night of sleep, I return to my problems and found a solution.
Thanks to give me your feedback if you think this solution is appropriate.
function only_current_author( $query ) {
if ( !is_admin() && $query->query_vars['post_type'] != 'nav_menu_item' ) {
global $wp_query;
$post_id = $wp_query->post->ID;
$post_type = $wp_query->post->post_type;
if( $post_id != 0 && $post_type == 'cpt_mdj' ) {
$post_author_id = get_post_field( 'post_author', $post_id );
$query->query_vars['author'] = $post_author_id;
}
}
}
add_action( 'pre_get_posts', 'only_current_author' );
So, the issue was that $post_id = get_queried_object_id(); returned null, from that it was impossible to pre filter the query with the author id.
Using $wp_query, I was able to get the post id, and then the author id.
Side question, what is the difference between $query and $wp_query, they are identical when I var_dump, but give different result.
Thanks!