I'm using WPML (Wordpress multilanguage plugin) with custom post and fields (with Advanced Custom Fields plugin) and I have this "issue":
I've a custom post with a custom field (text), I enter text in the field and save. Now I go to the translated post and see that the same custom field is empty. Then the fields ar not synched. Notice that instead the Tag field is well synched between the languages.
Someone can help? Thanks
I don´t think the saved value of a custom field is synced by default. Only the name of the variable etc.
So if you have a custom field and wan´t it to have the same value on all languages, simply don´t add that custom field to other languages. Just have it on the main language.
Then in the template you can use this, to allways get the value from main language:
<?php the_field('fieldname',lang_page_original_id($post->ID));?>
Then add this to functions.php
function lang_page_original_id($id){
if(function_exists('icl_object_id')) {
return icl_object_id($id,'page', false, "MAIN LANGUAGE CODE EX: SV");
} else {
return $id;
}
}
Here are ACF docs: http://www.advancedcustomfields.com/resources/multilingual-custom-fields/
But it doesn't work as well as you may expect. Syncing is only "one way" from original to translated versions. See: https://wordpress.stackexchange.com/questions/181338/fixed-values-for-same-post-translations/214120#214120 for more details.
You will need WPML Multilingual CMS in order to use sync feature.
Hi use this in your function.php works 100%:
function sync_field_meta( $post_id, $post, $update ) {
$post_type = get_post_type($post_id);
// use this if u have muti custom post type
$posts_type = array('your_custom_post_type1', 'your_custom_post_type2', 'your_custom_post_type3', 'your_custom_post_type4');
if( ! in_array($post_type, $posts_type)) return;
$en = apply_filters( 'wpml_object_id', $post_id, 'any', FALSE, 'en' );
$fr = apply_filters( 'wpml_object_id', $post_id, 'any', FALSE, 'fr' );
// your acf key like (field_58136c9dc9963) you can check documention
$field = get_field('acf_key',$post_id);
if($en){
update_field('acf_key',$field,$en);
}
if($fr){
update_field('acf_key',$field,$fr);
}
}
add_action( 'save_post', 'sync_field_meta', 10, 3 );
Related
I am using ACF to add fields to my vendors' dashboard profile pages. I currently have a test ACF field loading the field from only the WP Admin profile page on all the vendors' product listing page using this simple hook in my child theme's functions.php:
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() { ?>
<?php if(get_field('founded_on')) { ?>
<?php the_field('founded_on'); ?>
<?php }
}
Perhaps I'm pulling the wrong hook, but I can't seem to find the right hook in the Product Vendor frontend page.
I need help customizing it so that when you are on a vendor's product page, it pulls the ACF fields from that particular vendor's profile. Currently, it partially works; however it only pulls the WP main admin's data for all the different vendors'.
I know there is a way to pull the vendor's ID for each particular vendor's page and have it load their data for their page, but my php knowledge is very limited. I usually just hunt for existing code and tweak it. Unfortunately I haven't found any solutions that have worked, and this is the closest I've come to getting custom fields to work on a vendor's page.
Or if anyone can point me to a better solution to allow me to create customer fields for a vendor to fill out that will be loaded on their front end page, that would be great. I've tried Nicola Mustone's solution ( here ), which would have been perfect, except I couldn't get it to load the new custom fields on the vendor's store profile form page, nor have it load the fields into that vendor's storefront page. Based on comments, it only shows up for the site's Admin and only they can edit it. There's no visible way to have it load on the storefront, which defeats the purpose.
I imagine that the providers are users with a certain level within the WordPress system?
Considering that this is your case, the ACF fields need some additional parameters to become visible:
$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above
$value1 = the_field( 'my_field', $post_id );
$value2 = get_field( 'my_field', $post_id );
take some examples found in the ACF documentation, but in your particular case you have to pass the user's ID
the_field('founded_on', 'user_' . $user->ID );
echo get_field('founded_on', 'user_' . $user->ID );
function documentation the_field()
You need to pull the user ID of the user and then use the format user_{$user->ID} for the post ID as the second parameter of the ACF field.
If I understand your question, then this should work.
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() {
$user = wp_get_current_user();
if ( get_field( 'founded_on', 'user_' . $user->ID ) ) {
the_field( 'founded_on', 'user_' . $user->ID );
}
}
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 am using a free Real Estate Wordpress plugin from the repository but the support is paid.
Anyway, the plugin's "City" taxonomy is NOT hierarchical. I need to make it HIERARCHICAL so I can create counties with hierarchical cities under it. As you know, modifying plugin's files is not a possibility for known reasons (update overwriting).
I am looking for something like this to deploy in functions.php:
function change_post_object_label( $taxonomy_args ) {
$taxonomy_args->hierarchical = true;
}
add_action( 'taxonomy_hook', 'change_taxonomy_args' );
Does it exist? How can I set hierarchical to "true" for a given taxonomy without having to alter the php files?
If you are working on WordPress 4.4 or a newer version, you may use the register_taxonomy_args filter.
Add this to your functions.php, and don't forget to use the actual taxonomy slug.
function filter_register_taxonomy_args( $args, $taxonomy ) {
if ( $taxonomy === 'taxonomy_slug_here' ) {
$args['hierarchical'] = true;
}
return $args;
}
add_filter( 'register_taxonomy_args', 'filter_register_taxonomy_args', 10, 2 );
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
Is it possible to access pluggin's data from WordPress database with some Rest API. I saw WP REST API but it doesn't give access to pluggin's data.
More specifically I use LearnDash pluggin and I want to access courses infos but they doesn't provide any API to access it.
Thanks
Overall you need to search Google for things having to do with "Wordpress rest api custom post type" and eventually you'll be dealing with registering custom post types and endpoints in your theme's functions.php
Here is the manual: https://developer.wordpress.org/rest-api/extending-the-rest-api
As an example, here is my situation: I'm using WordPress (4.7) and The Events Calendar ("TEC") plugin. I'm querying the WP DB from an Android app using the REST API, however the only data that's available to REST is basic WP post stuff. I need to expose the custom data in the TEC plugin to the REST API. In order to do that, I've included the following into my functions.php in my theme:
functions.php
/* Exposing the custom post type of the plugin to the REST API.
* In this case, for The Tribe Events Calendar plugin, the custom post type is
* "tribe_events". For other plugins it will be different. */
add_action( 'init', 'my_custom_post_type_rest_support', 25 );
function my_custom_post_type_rest_support() {
global $wp_post_types;
//be sure to set this to the name of your post type!
$post_type_name = 'tribe_events';
if( isset( $wp_post_types[ $post_type_name ] ) ) {
$wp_post_types[$post_type_name]->show_in_rest = true;
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}
/* Exposing the custom taxonomy of the plugin to the REST API.
* In this case, for The Tribe Events Calendar plugin, the custom
* taxonomy is "tribe_events_cat". For other plugins it will be different.
*/
add_action( 'init', 'my_custom_taxonomy_rest_support', 25 );
function my_custom_taxonomy_rest_support() {
global $wp_taxonomies;
//be sure to set this to the name of your taxonomy!
$taxonomy_name = 'tribe_events_cat';
if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
$wp_taxonomies[ $taxonomy_name ]->show_in_rest = true;
// Optionally customize the rest_base or controller class
$wp_taxonomies[ $taxonomy_name ]->rest_base = $taxonomy_name;
$wp_taxonomies[ $taxonomy_name ]->rest_controller_class = 'WP_REST_Terms_Controller';
}
}
With the above in my functions.php, now I can query just the custom post type and taxonomy with the REST API. Here's a sample query:
http://www.mywebsite.com/wp-json/wp/v2/tribe_events?tribe_events_cat=64
However, I still don't see any of the custom plugin data per-event. All I see are the basic standard WP fields.
So now I have to add more to my functions.php:
/* Add specific endpoints to REST API.
* The TEC plugin has good documentation, and they have a
* list of the variables. With any other plugin, you might
* have to do more detective work. */
add_action( 'rest_api_init', 'slug_register_event_venue' );
function slug_register_event_venue() {
register_rest_field( 'tribe_events',
'_EventVenueID',
array(
'get_callback' => 'slug_get_event_venue',
'schema' => null
)
);
}
function slug_get_event_venue( $object, $field_name, $request ) {
$postId = tribe_get_venue_id( $object[ 'id' ]);
if ( class_exists( 'Tribe__Events__Pro__Geo_Loc' ) ) {
$output[ 'locid' ] = (float) $postId;
$output[ 'lat' ] = (float) get_post_meta( $postId, Tribe__Events__Pro__Geo_Loc::LAT, true );
$output[ 'lng' ] = (float) get_post_meta( $postId, Tribe__Events__Pro__Geo_Loc::LNG, true );
} else {
$output = array(
'locid' => 0,
'lat' => 0,
'lng' => 0,
);
}
return $output;
}
... and a bunch more endpoint definitions of which I'm excluding the code here.
Now when I run the same query from earlier, new data fields are present in the JSON: all of the endpoints I've added.
At this point I want to exclude a ton of things from the REST API output (all the miscellaneous WP stuff) and just return the event fields I'm interested in. To do that I just add a "&fields" parameter to my query, with fields seperated by comma. There's also a per_page and order parameters too.
http://www.mywebsite.com/wp-json/wp/v2/tribe_events?tribe_events_cat=64&per_page=100&order=asc&fields=id,title.rendered,_EventVenueID.lat,_EventVenueID.lng,_EventVenueID.locid,_EventStartDate.startdate,tribe_events_cat
This returns JSON data with only the data I'm interested in.
The TEC plugin maker has stated they'll be introducing official REST API support sometime soon. This means in the future I can probably eliminate all of this code in functions.php, and just utilize whatever interface the plugin maker comes up with... hopefully a nice sexy page inside the plugin settings.
Now that WP (4.7) has more or less a fully featured REST API, the ball is in the court of the plugin makers to build support for it. So this year you should see plugins be updated accordingly.
When you are creating the custom post type in WordPress just add one more parameter show_in_rest as true for the support the rest API for Custom post type.
/**
* Register a book post type, with REST API support
*
* Based on example at:
https://codex.wordpress.org/Function_Reference/register_post_type
*/
add_action( 'init', 'my_book_cpt' );
function my_book_cpt() {
$args = array(
'public' => true,
'show_in_rest' => true,
'label' => 'Books'
);
register_post_type( 'book', $args );
}
Please visit here.
Then you can use the custom endpoint by the URL like this:
https://example.com/wp-json/wp/v2/book