TWD
Forum Replies Created
-
Thank you so much for all your help.
REALLY appreciate it.
I’m going to stick with my PHP approach for now, since its only a smallish number of records.Here is the final code:
// only create a new external client ID if the post in new, NOT an update function set_external_client_id( $post_id, $post, $update ) { if ( wp_is_post_revision( $post_id ) ) { return; } if ( 'auction-client' == $post->post_type ) { if( !$update ){ $args = array( 'post_type' => 'auction-client', ); $myloop = new WP_Query( $args ); //loop through all posts of this post type to find the highest client id $find_highest_id = 0; while ( $myloop->have_posts() ) : $myloop->the_post(); $this_post_id = get_the_ID(); $external_client_id = get_post_meta( $this_post_id, 'wpcf-external-client-id', true ); if($external_client_id > $find_highest_id){ $find_highest_id = $external_client_id; } endwhile; wp_reset_postdata(); //take the highest value and increment it then save as client id for the new client $external_client_id = $find_highest_id + 1; update_post_meta( $post_id, 'wpcf-external-client-id', $external_client_id); } } } add_action( 'wp_insert_post', 'set_external_client_id', 10, 3 );
Forum: Developing with WordPress
In reply to: Custom loop isnt working correctlyThank you for your response.
Appreciate you sharing your skill with me.Terrific answer.
Thank you for taking the time to explain this.
That’s how others can learn!You said:
“2. You could also have the WP_Query so that the results are displayed in descending order of the value of the meta field.”I actually thought about this.
Is it MORE efficient to ask PHP to sort the results and then just pick the first result of the array? OR is it more efficient to loop through as I have done?“This way the first post in the loop will be the one with the highest value of ‘wpcf-external-client-id’. So you won’t need to loop through all the results; you’d be able to use just the first one.”
BTW could you just remind me how to access the first post?
This would be an array of “post objects” correct?Thank you.
That works!Now to expand on that to address the initial problem, how to find the highest External Client ID, increment it by “1” and save that value when the post is created (but not when updated).
Can you tell me what mistake I have made with this code?
The value is always saved as “1”.
function set_external_client_id( $post_id, $post, $update ) { if ( wp_is_post_revision( $post_id ) ) { return; } if ( 'auction-client' == $post->post_type ) { if( !$update ){ $args = array( 'post_type' => 'auction-client', 'post_status' => 'publish', ); $loop = new WP_Query( $args ); $find_highest_id = 0; while ( $loop->have_posts() ) : $loop->the_post(); $external_client_id = get_post_meta( $post_id, 'wpcf-external-client-id', true ); if($external_client_id > $find_highest_id){ $find_highest_id = $external_client_id; } endwhile; $external_client_id = $find_highest_id + 1; update_post_meta( $post_id, 'wpcf-external-client-id', $external_client_id); } } } add_action( 'wp_insert_post', 'set_external_client_id', 10, 3 );
Back to first principles.
I would expect this code to insert the value ‘1234’ on update.
But nothing happens.
Yes, I am a coding noob.function set_external_client_id( $post_id, $post ){ if ( 'auction-client' == $post->post_type ) { //only proceed if this is a Client post type do_action( 'wp_insert_post', $post_id, $post, $update ); if($update){ $external_client_id = 1234; update_post_meta( $post_id, 'wpcf-external-client-id', $external_client_id); } } } add_action( 'save_post', 'set_external_client_id', 30, 2 );
Forum: Plugins
In reply to: [GAinWP Google Analytics Integration for WordPress] Plugin brokenThis plugin has been abandoned.
Google SiteKit is now the “least worst” option for dashboard analytics.
Forum: Developing with WordPress
In reply to: How to show “Published” pages on Dashboard by defaultThanks Ian.
You are right. It works for me in 5.5.x
*solved*
Forum: Plugins
In reply to: [GAinWP Google Analytics Integration for WordPress] New GA4 compatibility?following
Forum: Developing with WordPress
In reply to: How to write a simple redirect functionI should have mentioned that I am working in an NGINX environment.
ie. no .htaccessI’m looking for a PHP custom code solution that I can add to my miscellaneous custom functions plugin.
Forum: Plugins
In reply to: [Lordicon Animated Icons] ElementorI think the plugin has been abandoned.
OK. Thanks. I will dig into it and see how easy it is to write a custom plugin.
Brilliant!
Exactly what I was looking for.Forum: Developing with WordPress
In reply to: multilingual FormsWPML integrates with most form plugins.
Not sure what you mean by “stories”.Same!
Why hasn’t there been an update bump for six months?Forum: Developing with WordPress
In reply to: MultiSite code snippet. Can’t upload ANY media.Solved.
The problem apparently, is that the return $mimes was in the wrong place.
It needs to be at the very bottom of the function, outside the conditional statements.Otherwise, when testing either as a Super Admin or in any situation where the conditional statements return false, no $mimes are handed back by the plugin.
Here is the working code.
function restrict_mime($mimes) { if ( function_exists('is_plugin_active_for_network') ){ if ( is_plugin_active_for_network( 'wp-ultimo/wp-ultimo.php' ) ) { if ( !is_super_admin() ) { //get user id $user_id = get_current_user_id(); //define the Premium Pro plan ID $plan_id = 52; // restrict MIME types if the current user doesnt have Premium Pro plan if ( !wu_has_plan( $user_id, $plan_id ) ) { $mimes = array( 'jpg|jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'pdf' => 'application/pdf', 'txt' => 'text/plain', 'csv' => 'text/csv', 'doc' => 'application/msword', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'ppt' => 'application/vnd.ms-powerpoint', 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'xls' => 'application/vnd.ms-excel', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ); } } } } return $mimes; } add_filter('upload_mimes','restrict_mime');
- This reply was modified 4 years, 6 months ago by TWD.