Tagged: custom post statuses, get_post_meta
- This topic has 7 replies, 3 voices, and was last updated 5 years, 10 months ago by
wpmusketeer.
-
AuthorPosts
-
-
March 4, 2016 at 10:46 am #6027
wpmusketeerMemberHi,
I have a CPT ‘job’ with a bunch of Piklist custom fields. The CPT also has custom post statuses that consist of: ‘draft’, ‘processing’, ‘complete’.
Jobs are fetched via an external API and populated into the WordPress CPT using
wp_insert_post()orwp_update_post()with'post_status' => 'processing'as one of the arguments.Everything appears correctly in the WordPress admin, including all the custom Piklist fields that are populated from the remote API.
On the frontend, using a custom
WP_Queryloop, I can call the standard WordPress fields using functions likethe_content(),the_title()etc., but callingget_post_meta()on the Piklist custom fields doesn’t work.Using something like this:
$priority = get_post_meta( get_the_ID(), PREFIX . 'job_priority', true );Just returns an empty string. If I change the last parameter to ‘false’ it returns an empty array.
The same is true for simple text fields and radio fields.
However. If I manually go into a Job through the WordPress admin and click save, then go back to the frontend and refresh, the one Job I manually saved will now return the post meta correctly. The rest of the Jobs still won’t though.
Have you come across this before? Is there something else I need to do after
wp_insert_post()andwp_update_post()?Thanks in advance,
Dave
-
March 4, 2016 at 3:59 pm #6029
wpmusketeerMemberHere’s how I’m registering the Job CPT including the custom statuses:
add_filter( 'piklist_post_types', 'wpm_register_post_types' ); /** * Register Custom Post Types. * * @param array $post_types Existing array of CPTs. * @return array Amended array of CPTs */ function wpm_register_post_types( $post_types ) { $post_types['job'] = array( 'labels' => piklist( 'post_type_labels', 'Job' ), 'public' => true, 'supports' => array( 'title', 'editor', 'author', 'comments', ), 'menu_icon' => 'dashicons-hammer', 'show_in_rest' => true, 'status' => array( 'draft' => array( 'label' => 'Draft' ), 'processing' => array( 'label' => 'Processing' ), 'complete' => array( 'label' => 'Complete' ), ), ); return $post_types; }Also, I notice in the WordPress admin that when you click “Save” on any CPT created with Piklist, the button briefly flashes “Publish” then reverts back to “Save”. Is that normal?
Thanks again,
Dave
-
March 5, 2016 at 4:13 pm #6033
cjwebdesignttMemberHi, I have come across this before and its not because of post statuses. It seems from your explanation that you are not saving the meta data.
wp_insert_postandwp_update_postonly save the standard fields you need to save the post_meta usingadd_post_metaandupdate_post_meta.Can you post the function that creates the post and saves the post meta data so I can get a better understanding. An example of how I did this is below:
/** * Insert the new post and associated post meta */ $fullname = $_POST['first-name'] . ' ' . $_POST['last-name']; $entry = array( 'post_title' => wp_strip_all_tags( $fullname ), 'post_type' => 'tgu_form_entry', 'post_status' => 'publish', 'post_author' => $user_id ); $the_post_id = wp_insert_post( $entry ); if($the_post_id) { $wp_tgu_session['user_temp'] = $the_post_id; //Related Job add_post_meta( $the_post_id, '_tgu_related_job', $_POST['job-id'], true ); //Personal Info post meta add_post_meta( $the_post_id, 'tgu_job_first_name', sanitize_text_field($_POST['first-name']), true ); add_post_meta( $the_post_id, 'tgu_job_last_name', sanitize_text_field($_POST['last-name']), true ); add_post_meta( $the_post_id, 'tgu_job_email', sanitize_email( $_POST['email']), true ); add_post_meta( $the_post_id, 'tgu_job_phone', $_POST['phone-number'], true ); //add_post_meta( $the_post_id, 'tgu_job_region', $_POST['region'], true ); add_post_meta( $the_post_id, 'tgu_job_city', $_POST['city-borough'], true ); // Custom Questions for ($i=1; $i < 6; $i++) { if( ! empty( $_POST['tgu_cus_q_' . $i] ) ) { add_post_meta ($the_post_id, 'tgu_cus_q_' . $i, $_POST['tgu_cus_q_' . $i]); } } /** * Upload Resume */ // These files need to be included as dependencies when on the front end. require_once( ABSPATH . 'wp-admin/includes/image.php' ); require_once( ABSPATH . 'wp-admin/includes/file.php' ); require_once( ABSPATH . 'wp-admin/includes/media.php' ); $attachment_id = media_handle_upload( 'upload-resume', $the_post_id ); /** * Handle Add More/ Repeater Fields */ //There are arrays //Education $institution = $_POST['tguinstitution']; $year = $_POST['tguyear']; $degree = $_POST['tgudegree']; $course = $_POST['tgucourse']; $grade = $_POST['tgugrade']; if( count($institution) > 0 ) { for ($i=0; $i < count($institution); $i++) { add_post_meta( $the_post_id, 'tgu_app_ins_' . $i, $institution[$i]['name'], true ); } } if( count($year) > 0 ) { for ($i=0; $i < count($year); $i++) { add_post_meta( $the_post_id, 'tgu_app_yr_' . $i, $year[$i]['name'], true ); } } if( count($degree) > 0 ) { for ($i=0; $i < count($degree); $i++) { add_post_meta( $the_post_id, 'tgu_app_deg_' . $i, $degree[$i]['name'], true ); } } if( count($course) > 0 ) { for ($i=0; $i < count($course); $i++) { add_post_meta( $the_post_id, 'tgu_app_crse_' . $i, $course[$i]['name'], true ); } } if( count($grade) > 0 ) { for ($i=0; $i < count($grade); $i++) { add_post_meta( $the_post_id, 'tgu_app_grade_' . $i, $grade[$i]['name'], true ); } } //----------- //Experience $company = $_POST['tgucompany']; $industry = $_POST['tguindustry']; $jobtitle = $_POST['tguexpjobtitle']; $year = $_POST['tguexpyear']; $role = $_POST['tguexproles']; if( count($company) > 0 ) { for ($i=0; $i < count($company); $i++) { add_post_meta( $the_post_id, 'tgu_app_exp_co_' . $i, $company[$i]['name'], true ); } } if( count($industry) > 0 ) { for ($i=0; $i < count($industry); $i++) { add_post_meta( $the_post_id, 'tgu_app_exp_in_' . $i, $industry[$i]['name'], true ); } } if( count($jobtitle) > 0 ) { for ($i=0; $i < count($jobtitle); $i++) { add_post_meta( $the_post_id, 'tgu_app_exp_job_' . $i, $jobtitle[$i]['name'], true ); } } if( count($year) > 0 ) { for ($i=0; $i < count($year); $i++) { add_post_meta( $the_post_id, 'tgu_app_exp_yr_' . $i, $year[$i]['name'], true ); } } if( count($role) > 0 ) { for ($i=0; $i < count($role); $i++) { add_post_meta( $the_post_id, 'tgu_app_exp_rr_' . $i, $role[$i]['name'], true ); } } //References $reffulln = $_POST['reffn']; $refco = $_POST['refco']; $reftitle = $_POST['reftitle']; $refPhone = $_POST['refphone']; if( count($reffulln) > 0 ) { for ($i=0; $i < count($reffulln); $i++) { add_post_meta( $the_post_id, 'tgu_ref_fn_' . $i, $reffulln[$i]['name'], true ); } } if( count($refco) > 0 ) { for ($i=0; $i < count($refco); $i++) { add_post_meta( $the_post_id, 'tgu_ref_co_' . $i, $refco[$i]['name'], true ); } } if( count($reftitle) > 0 ) { for ($i=0; $i < count($reftitle); $i++) { add_post_meta( $the_post_id, 'tgu_ref_job_' . $i, $reftitle[$i]['name'], true ); } } if( count($refPhone) > 0 ) { for ($i=0; $i < count($refPhone); $i++) { add_post_meta( $the_post_id, 'tgu_ref_ph_' . $i, $refPhone[$i]['name'], true ); } } return $the_post_id; } else { throw new Exception('There was a problem creating your application'); }Notice that on successful wp_insert_post you will get the post_id, therefore if the post_id is present then you update_post_meta.
-
March 7, 2016 at 4:52 pm #6035
wpmusketeerMemberHi, thanks for your reply. I have fixed the issue but there’s something a little buggy that may be down to WordPress itself or Piklist, I’m not sure.
I was using the new syntax for
wp_insert_post()andwp_update_post()whereby you can update post meta from within the aforementioned functions. Like this:$job_details = array( 'post_title' => $order_number, 'post_status' => 'processing', 'post_type' => 'job', 'post_content' => $job_notes, 'meta_input' => array( PREFIX . 'stitch_id' => $stitch_job_id, PREFIX . 'stitch_invoice_status' => $stitch_invoice_status, PREFIX . 'stitch_payment_status' => $stitch_payment_status, PREFIX . 'stitch_packing_status' => $stitch_packing_status, PREFIX . 'stitch_shipping_status' => $stitch_shipping_status, PREFIX . 'stitch_job_total' => $total, PREFIX . 'related_contact' => $related_contact_id, ), ); $new_job = wp_insert_post( $job_details, true );And indeed, for those meta fields you see above it worked fine. They are all fields created by Piklist (dropdowns, a text field and a dropdown field for a related CPT) that are being passed data that is returned from a request to a remote API (Stitch Labs).
However, there are also a few radio fields on the Job CPT. When I tried to update them in the same way as above I experienced the issues mentioned in the original post.
I’ve found that reverting to the older method just for the troublesome fields as per your post, where you do the
wp_insert_post()then use the returned ID to doupdate_post_meta()does work.I don’t quite understand why this is, as looking at WordPress’s core code for the new syntax all it does is cycle through the ‘meta_input’ key/value pairs and call
update_post_meta()on them anyway?Thanks again for your time.
-Dave
-
March 14, 2016 at 8:22 pm #6079
JasonKeymasterInteresting, there isn’t anything particularly complicated going on here, so it should be a simple bug. Out of curiosity, what’s saving to the database when you save using the Piklist fields? I assume you’re using the PREFIX constant for those as well?
-
March 15, 2016 at 6:52 am #6083
wpmusketeerMemberHi Jason,
The code I posted above is how the
$job_detailsarray looks now. The fields using the PREFIX constant are all Piklist fields. They are the Piklist fields that always worked insidewp_insert_post()(and still do). They are all simple text fields apart fromPREFIX . 'related_contact'which is a dropdown relate field.The fields I had to pull out of that array, and update separately via
update_post_meta()were all radio fields. There is a difference between the above fields that work insidewp_insert_post()and the radio fields (that don’t), which might explain where the bug lies. The radio fields all had default values, whereas the above fields which worked did not.Here’s an example of how I’m declaring a radio field:
piklist( 'field', array( 'type' => 'radio', 'field' => PREFIX . 'proof_status', 'list' => false, 'value' => 'proof_needed', 'label' => __( 'Proof Status', 'core-functionality' ), 'choices' => array( 'proof_needed' => 'Needs Proof', 'proof_sent' => 'Proof Sent', 'proof_not_required' => 'Not Required', 'proof_approved' => 'Approved', ), ) ); -
March 17, 2016 at 1:11 pm #6108
JasonKeymasterOk, thanks. I mean, what’s really interesting to me is that the bug is that get_post_meta isn’t working, and that’s a very simple deal. If Piklist is presenting everything correctly, then I would say the problem lies somewhere in wp_insert_post, not Piklist. After wp_insert_post is called, what’s in the meta table for that new post? Is there something wrong with the field name? Is it there at all? Is it an empty value? Radio fields should simply store a single row with the value provided.
I would start by analyzing the database after wp_insert_post is called and see why get_post_meta is returning empty (either there’s no value or there’s no row). Then I would go into the WordPress core and debug what’s happening in that function and see if there’s something strange going on in there — perhaps a filter is stripping the meta for some reason.
Hope this helps!
-
March 21, 2016 at 9:27 am #6130
wpmusketeerMemberHi Jason, I’ve been off this project the last few days so I haven’t had a chance to investigate any further. I’ll let you know what I found out when I get back to it though!
Cheers for your time 🙂
-
-
AuthorPosts
- You must be logged in to reply to this topic.