How is clicking 'update' on a post different from programatically creating posts? - wordpress

hoping for some advice.
I'm programmatically inserting a large number of posts into wordpress from a JSON feed. The wp_insert_post function is working brilliantly and the posts are created, along with correctly populated Advanced Custom Fields meta data.
We have a strange issue by which until we manually click "update" on a single post the custom fields aren't available using a JSON API plugin.
I've tried updating all via the bulk editor, as well as calling wp_update_post after the JSON import. It's as if the act of clicking "update" on a single post saves the post in a different fashion.
Can anyone advise why this would be the case? Any advice or pointing in the right direction would be greatly appreciated!
EDIT: the code we're using to update our post meta...
function __update_post_meta( $post_id, $field_name, $value = '' ) {
if ( empty( $value ) OR ! $value )
{
delete_post_meta( $post_id, $field_name );
}
elseif ( ! get_post_meta( $post_id, $field_name ) )
{
add_post_meta( $post_id, $field_name, $value );
}
else
{
update_post_meta( $post_id, $field_name, $value );
}
}

So we resolved the particular issue we were having.
When a post is programmatically created with ACF fields, the posts are not labelled in the same way as standard post meta. Read here for more info on that!
Updating a post manually creates the necessary "aliases". Until that point, if you want to get the info out, you need to reference ACF's initial "fieldXXXXXXXX" post meta key.

Related

ACF Form Update duplicates post

i’m having a small issue.
Using a CPT, where no title is available, so i’m using the following code to make the title out of 2 ACF fields (first name / last name).
What is happening is that instead of updating, it’s making a new post with the new information. When i disable the script, everything works as intended.
Is there something that I could add to this script so that post update with forms work correctly?
function doctors_title_updater( $post_id )
{
if ( get_post_type( $post_id ) == 'doctors' ) {
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = get_field( 'doctors_lname', $post_id ) . substr(get_field( 'doctors_fname', $post_id ), 0, 30) ;
wp_update_post( $my_post );
}
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'doctors_title_updater', 20);
The code works pefectly fine when i'm editing the post from the back-end and hit update. The issue is only when using the submit from the front-end form.

How to save custom meta only for review data to see only in "Preview Changes", not the actual post in the front end

I am trying to add custom meta and see at "Preview Changes". I can see the changes but also changes apply to the actual post at Front end. I want the changes will update to the actual post when it Publish or Update not at "Preview Changes" click. Please help.
I have followed this plugin.
function my_plugin_save_post( $post_id, $post ) {
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$parent = get_post( $parent_id );
$my_meta = get_post_meta( $parent->ID, 'my_meta', true );
if ( false !== $my_meta )
add_metadata( 'post', $post_id, 'my_meta', $my_meta );
}
} add_action( 'save_post', 'my_plugin_save_post' );
The following code will prevent your meta data from saving on preview but You won't be able to preview published posts with your metadata. Honestly I'm trying to figure out this situation myself :/
<?php // In your save metabox data function, near the top...
if (isset( $_POST['wp-preview'] ) && 'dopreview' == $_POST['wp-preview'] ) {
if(get_post_status($post_id) == 'publish'){
return; // This way we can still preview draft / scheduled posts
}
}
Honestly, I'd use this code and set your post briefly to draft or private while editing / previewing and publish them as normal when you're done.

WooCommerce - Making a checkout field required

on my baked goods site, I have WooCommerce installed and another plugin called Order Delivery Date for WooCommerce.
I installed the second plugin so my customers would be able to choose a delivery date for their items, however, I am trying to make the form field a required field. So far, I've just been able to make the field look like a required field, but have not figured out how to make sure that it is actually enforced. Any ideas?
Also, if anyone is familiar with WooCommerce, do you know how I would be able to make it so that customers receive this delivery date information in their order confirmation emails?
Thank you in advance!
My site: www.monpetitfour.com
You should try to had something like that :
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// You can make your own control here
if ( ! $_POST[ 'e_deliverydate' ] )
wc_add_notice( __( 'Please select a delivery date' ), 'error' );
}
For the email, the easiest is to save the meta value ( I think it's already done by your plugin). Then you need to copy the template email (customer-processing-order.php) on your theme and change in the template :
<?php $delivery_date = get_post_meta( $order->id, 'custom_field_date', true);
// If the plugin is well developed, you can't directly use magic getters :
// $delivery_date = $order->e_deliverydate;
// Can only by use if the post meta start with _
?>
Your delivery date is <?php echo $delivery_date ?>
You can also use
date_i18n( woocommerce_date_format(), strtotime( $delivery_date ) );
In order to format the date correctly.
On the code above, you just need to find the name of the custom field used by the plugin ( you can search easily on the table wp_postmeta searching by an existing order (should be _e_deliverydate).
Add the following code to your theme's functions.php file
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['e_deliverydate'] )
wc_add_notice( __( 'Please select a delivery date.' ), 'error' );
}
Now to get the email to show the custom field,
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');
function my_woocommerce_email_order_meta_keys( $keys ) {
$keys['Delivery Date'] = '_e_deliverydate';
return $keys;
}
EDIT : Seems the field value isn't being saved to the database, try saving it explicitly
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['e_deliverydate'] ) ) {
update_post_meta( $order_id, '_e_deliverydate', sanitize_text_field( $_POST['e_deliverydate'] ) );
}
}

Pass Woocomerce session data to databse or create custom cart data and passing it to db

I need to add an expiry date to each product that are added to the cart and like to see it in order details after order is complete.
$woocommerce->cart->add_to_cart($each_item[0],$each_item[1],'','',array('__expirydate',$d_expiry));
But the array, array('__expirydate',$d_expiry) i am passing is not showing with my cart. or adding with the woocommerce session.
So i set session manually using
WC()->session->set( 'expiry'.$each_item[0], $d_expiry );
And is working completely fine.But When Checkout that data is not stored anywhere.How to add it.
The session is cleared when the order is completed. You probably need to save some meta information for the ordered item, which you can do via the woocommerce_add_order_item_meta hook.
add_action( 'woocommerce_add_order_item_meta', 'so_add_order_item_meta', 10, 3 );
function so_add_order_item_meta( $order_item_id, $cart_item, $cart_item_key ) {
if ( isset( $cart_item[ '__expirydate' ] ) && ! empty( $cart_item[ '__expirydate' ] ) ) {
wc_add_order_item_meta( $order_item_id, __( 'Expiration date' ), $cart_item[ '__expirydate' ] );
}
}
Keep in mind, this isn't a cut and paste solution, but a nudge down the right path.
Also, in lieu of your session approach you could probably look into using the following combination of filters: woocommerce_add_cart_item_data, woocommerce_add_cart_item and woocommerce_get_cart_item_from_session to add data to the items in the cart (plus that would work with the code I've posted).
Good luck.

WordPress 4.0 - Display author's content on custom post type

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!

Resources