Forum Replies Created

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • in reply to: get_post_meta not working on CPT with custom post statuses #6130
    wpmusketeer
    Member

    Hi 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 🙂

    in reply to: Frontend forms in template files #6098
    wpmusketeer
    Member

    That looks like it’s worth investigating. I’ll let you know how I get on with manually updating some awesomeness 😉

    Thanks Mike!

    in reply to: Frontend forms in template files #6094
    wpmusketeer
    Member

    That’s what I’ve done for the main fields, just need to find a way that does the same for the ID of the CPT to update!

    in reply to: Frontend forms in template files #6092
    wpmusketeer
    Member

    Hi Mike,

    Thanks for the reply.

    I’m pretty sure Piklist can’t handle this situation with the way frontend forms currently work because it requires the ID of the post you want to edit to be passed to the form in the form of a GET URL param.

    I did a bit of sleuthing to see if Piklist generated a different <form> tag, or added an extra post_id field when the GET URL param was present but there wasn’t anything obvious I could see.

    With multiple forms on the page I don’t know how you can get the correct ID passed to the form as it is rendered to make sure it POSTs to the right CPT instance.

    I’ve tried adding a hidden field in the form PHP file for the ID of the post to update (populated with get_the_id()) but that doesn’t seem to work.

    Also, I found the fields aren’t correctly pre-populated with the DB values for the current instance of the CPT. However, I was surprised to discover that by using 'value' => get_post_meta( get_the_ID(), PREFIX . 'accounts_status', true ), in the form PHP file the fields were pre-populated correctly for each instance!

    Anyway, here’s the code:

    parts/forms/job-edit.php

    
    <?php
    /**
     Title: Job List Edit Form
     Method: post
     Message: Job updated
     */
    
    piklist( 'field', array(
    		'type'  => 'hidden',
    		'scope' => 'post',
    		'field' => 'post_type',
    		'value' => 'job',
    	)
    );
    
    piklist( 'field', array(
    		'type'  => 'select',
    		'field' => PREFIX . 'accounts_status',
    		'scope' => 'post_meta',
    		'value' => get_post_meta( get_the_id(), PREFIX . 'accounts_status', true ),
    		'label' => __( 'Accounts Status', 'core-functionality' ),
    		'choices' => array(
    			'accounts_quote'   => 'Quote',
    			'accounts_hold'    => 'Unpaid Hold',
    			'accounts_release' => 'Unpaid Release',
    			'accounts_paid'    => 'Paid',
    		),
    	)
    );
    
    piklist( 'field', array(
    		'type'  => 'select',
    		'field' => PREFIX . 'proof_status',
    		'scope' => 'post_meta',
    		'value' => get_post_meta( get_the_id(), PREFIX . 'proof_status', true ),
    		'label' => __( 'Proof Status', 'core-functionality' ),
    		'choices' => array(
    			'proof_needed'       => 'Needs Proof',
    			'proof_sent'         => 'Proof Sent',
    			'proof_not_required' => 'Not Required',
    			'proof_approved'     => 'Approved',
    		),
    	)
    );
    
    piklist( 'field', array(
    		'type'  => 'select',
    		'field' => PREFIX . 'print_status',
    		'scope' => 'post_meta',
    		'value' => get_post_meta( get_the_id(), PREFIX . 'print_status', true ),
    		'label' => __( 'Print Status', 'core-functionality' ),
    		'choices' => array(
    			'printed_no'      => 'Not Printed',
    			'printed_yes'     => 'Printed',
    			'printed_reprint' => 'Needs Reprint',
    		),
    	)
    );
    
    piklist( 'field', array(
    		'type'  => 'select',
    		'field' => PREFIX . 'shipping_status',
    		'scope' => 'post_meta',
    		'value' => get_post_meta( get_the_id(), PREFIX . 'shipping_status', true ),
    		'label' => __( 'Shipping Status', 'core-functionality' ),
    		'choices' => array(
    			'shipped_no'   => 'Not Shipped',
    			'shipped_yes'  => 'Shipped',
    			'shipped_part' => 'Part Shipped',
    		),
    	)
    );
    
    piklist( 'field', array(
    		'type'  => 'submit',
    		'field' => 'submit',
    		'value' => 'Update',
    	)
    );
    

    jobs-list.php (template part)

    
    <?php
    /**
     * Render a list of Jobs.
     *
     * @package    Musket
     * @author     WP Musketeer - Dave Dean
     * @link       http://www.wpmusketeer.com
     * @copyright  Dave Dean, Moortor Design Ltd.
     * @license    GPL-2.0+
     */
    
    $args = array(
    	'post_type'      => array( 'job' ),
    	'posts_per_page' => -1,
    	'post_status'    => array( 'processing' ),
    	'meta_key'       => PREFIX . 'job_priority',
    	'orderby'        => 'meta_value_num',
    	'order'          => 'ASC',
    );
    
    $jobs = new WP_Query( $args );
    ?>
    
    <?php //echo do_shortcode( '[piklist_form form="job-edit" add_on="core-functionality"]' ); ?>
    
    <div class="job-board">
    
    	<ul class="job-header-row" id="jobHeaderRow">
    		<li>↕</li>
    		<li>Contact</li>
    		<li>Account Status</li>
    		<li>Proof Status</li>
    		<li>Print Status</li>
    		<li>Shipping Status</li>
    		<li>Notes</li>
    		<li>Actions</li>
    	</ul>
    
    	<ul class="job-list" id="sortableJobList">
    
    		<?php
    		if ( $jobs->have_posts() ) {
    			while ( $jobs->have_posts() ) : $jobs->the_post();
    				$priority = get_post_meta( get_the_ID(), PREFIX . 'job_priority', true );
    				$accounts_status = get_post_meta( get_the_ID(), PREFIX . 'accounts_status', true );
    				$proof_status = get_post_meta( get_the_ID(), PREFIX . 'proof_status', true );
    				$print_status = get_post_meta( get_the_ID(), PREFIX . 'print_status', true );
    				$shipping_status = get_post_meta( get_the_ID(), PREFIX . 'shipping_status', true );
    				$contact = get_post( $post->{ PREFIX . 'related_contact' } );
    				?>
    				<li class="job" data-id="<?php echo esc_attr( get_the_ID() ); ?>">
    					<div class="job-priority"><?php echo esc_html( $priority ); ?></div>
    					<ul class="job-details">
    						<li class="job-contact">
    							<div class="contact-name"><?php echo esc_html( $contact->post_title ); ?></div>
    							<div class="order-number"><?php echo the_title(); ?></div>
    						</li>
    						<li class="job-accounts-status">
    							<?php
    							echo esc_html( wpm_get_status_map( $accounts_status ) );
    							echo wpm_get_tick_icon_map( $accounts_status );
    							?>
    
    						</li>
    						<li class="job-proof-status">
    							<?php
    							echo esc_html( wpm_get_status_map( $proof_status ) );
    							echo wpm_get_tick_icon_map( $proof_status );
    							?>
    						</li>
    						<li class="job-print-status">
    							<?php
    							echo esc_html( wpm_get_status_map( $print_status ) );
    							echo wpm_get_tick_icon_map( $print_status );
    							?>
    						</li>
    						<li class="job-shipping-status">
    							<?php
    							echo esc_html( wpm_get_status_map( $shipping_status ) );
    							echo wpm_get_tick_icon_map( $shipping_status );
    							?>
    						</li>
    						<li class="job-notes">
    							<?php echo the_excerpt(); ?>
    						</li>
    						<li class="job-actions">
    							<a class="job-edit-button button" data-id="<?php echo esc_attr( get_the_ID() ); ?>" href="javascript:;">Edit</a>
    						</li>
    					</ul>
    					<div class="job-edit-<?php echo esc_attr( get_the_ID() ); ?>">
    						<form method="post" action="<?php echo esc_attr( site_url() ); ?>?_post[ID]=<?php echo esc_attr( get_the_ID() ); ?>" enctype="multipart/form-data" id="core_functionality_job_edit" autocomplete="off" data-piklist-form="true" class="piklist-form ">
    							<?php piklist::render( 'forms/job-edit' ); ?>
    						</form>
    					</div>
    				</li>
    				<?php
    			endwhile;
    		} else {
    			wpm_frontend_notice( 'No jobs found.', 'notice-warning is-dismissable' );
    		}
    		?>
    	</ul>
    </div>
    
    <?php
    wp_reset_postdata();
    

    You can see I’m adding the <form> tag manually in that example. Without it, no <form> tag renders. I’ve tried different actions for the form but all to no avail.

    in reply to: Frontend forms in template files #6089
    wpmusketeer
    Member

    No, that’s not going to work because of course the _post[ID]=123 URL param needs to be passed to the form as a GET as it’s rendered, not as a POST in the form action once it’s submitted.

    in reply to: Frontend forms in template files #6088
    wpmusketeer
    Member

    Actually, the above behaviour may just be perfect for my use-case because I can output the <form> tag myself and make sure it POSTs to a URL that passes the ID as a URL parameter.

    Let me test…

    in reply to: Frontend forms in template files #6084
    wpmusketeer
    Member

    Geddon Mike! Traditional Cornish greeting that 😉

    You certainly fanned the flame of my interest in Piklist, so thanks.

    I think this is a very common use-case in the world of SPAs, so it would be great if Piklist supported it, since Piklist is aiming to turn WordPress into a better platform for developing web apps.

    Thanks for tipping me off to piklist::render( 'forms/job' ); – where did you find out about that? The disconnect between the Piklist docs and trunk is Piklist’s single greatest limiting factor in my opinion.

    Using that function does allow the page to load, so it’s getting closer. However, the forms still don’t work as you can’t submit them. Inspecting the page it’s pretty obvious why: the actual <form /> element is missing!

    Screenshot of Piklist function not generating a form element

    Is that expected behaviour for the function? Is there another function you’re supposed to call to produce the <form /> element?

    in reply to: get_post_meta not working on CPT with custom post statuses #6083
    wpmusketeer
    Member

    Hi Jason,

    The code I posted above is how the $job_details array looks now. The fields using the PREFIX constant are all Piklist fields. They are the Piklist fields that always worked inside wp_insert_post() (and still do). They are all simple text fields apart from PREFIX . '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 inside wp_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',
    		),
    	)
    );
    
    in reply to: Frontend forms in template files #6064
    wpmusketeer
    Member

    Hi jrcreative,

    I think you’re on the right track. Limiting the query to a single Job lets the page load.

    I can’t see why multiple versions of the same form would cause a problem though, unless it’s something to do with the Piklist JavaScript on the page that is only expecting to see 1 instance of a form.

    There shouldn’t be anything wrong with having multiple instances of a form… after all, only 1 will ever be submitted at a time.

    The only field that should change is the ID it posts to. I’m still not sure how you’re supposed to pass the ID. In a hidden field perhaps? I don’t know what you mean when you say “pass the post id to the shortcode”.

    If the field names were made unique for each instance of the form as you suggest then all the forms would be attempting to update non-existant post meta!

    I’m guessing Piklist’s frontend forms can’t currently deal with this use-case, which is a shame because it’s likely to be a common one. I think I’ll give Caldera Forms a quick shot. They have an extension that lets you post meta to a CPT.

    Cheers,

    Dave

    in reply to: Frontend forms in template files #6057
    wpmusketeer
    Member

    Hmm… Been playing around a bit more. It’s very difficult to debug because it completely crashes the browser tab in Chrome and Safari. On Firefox I’ve found it throws up an error dialogue saying “Unresponsive script: jQuery”.

    I take it Piklist is using some JavaScript on frontend forms? In the Firefox inspector I can see piklist.js in the JS resources so I guess something here is clashing with jQuery.

    The divs with the Piklist forms are hidden by default. I have some JS that shows them when an edit button is clicked but I’ve completely removed that JS and the problem persists.

    wpmusketeer
    Member

    Hi, 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() and wp_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 do update_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

    wpmusketeer
    Member

    Here’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

    in reply to: Access related posts from current post #6023
    wpmusketeer
    Member

    Penny. Dropped. 🙂

    It appears to be working now. Thanks so much Jason!

    in reply to: Access related posts from current post #6014
    wpmusketeer
    Member

    Hi Steve,

    Thanks for getting back to me. I’m already on the version you linked to. I copy pasted the downloaded directory anyway just to be sure.

    Still no success 🙁

    Do you have to define a relate field on both CPTs? Currently I’m only defining the relationship on the Job.

    Can you tell me what changes I should be seeing in the database so I can better understand what’s supposed to happen and what’s not happening currently?

    Cheers,

    Dave

    in reply to: Access related posts from current post #5992
    wpmusketeer
    Member

    Doh (_8(|)

    Nope, it’s not working. On the edit Job page it is returning ALL Contacts (even ones that aren’t related) and on the edit Contact page it’s returning ALL Jobs (even ones that aren’t related).

    🙁

Viewing 15 posts - 1 through 15 (of 18 total)