Loop through incrementing custom fields

How can I loop through the fields and display the content without having to deplicate it? This for loop will cycle through the four field names to avoid duplication. <?php for ($i=1; $i<=4; $i++) { $fieldName=”featured_content_” . $i; $featuredContentPostID = get_field($fieldName)->ID; $selectedFeaturedContent = get_post( $featuredContentPostID ); ?> <li> <a href=”https://wordpress.stackexchange.com/questions/204839/<?php echo esc_url( get_permalink($featuredContentPostID) ); ?>”> … Read more

Woocommerce frontend edit custom fields

you don’t have to edit the order-details.php page the woocommerce_order_details_after_order_table action will achieve the same results. add_action( ‘woocommerce_order_details_after_order_table’, ‘namespace\custom_field_display_cust_order_meta’, 10, 1 ); function custom_field_display_cust_order_meta( $order ) { echo ‘<form [whatever]>’; /* translators: whatever */ echo ‘<p>’ . sprintf( __( ‘<strong>Property 1:</strong> %1$s (%2$s)’ ), ‘<input [whatever]>’ . get_post_meta( $order->get_order_number(), ‘property 1.1’, true ) . ‘</input>’, … Read more

Convert all dates stored in custom field

Find the correct key for game_date_key and run this once: global $wpdb; $posts = $wpdb->get_results(“SELECT ID FROM “.$wpdb->prefix.”posts” WHERE post_type=”post”); foreach ($posts as $post) { $old_date = get_post_meta($post->ID,’game_date_key’,true); $new_date = date(‘d/m/Y’, strtotime($old_date)); update_post_meta($post->ID,’game_date_key’,$new_date,$old_date); } (I assume you will have the sense to backup your database first just in case. 🙂

Save all custom field data into one “master” custom field

This is assuming each of the fields is a single value and you want to create a list for display… add_action(‘edit_post’,’custom_combine_fields’); function custom_combine_fields($post_id) { $pirates = get_post_meta($post_id,’pirates’,true); $robots = get_post_meta($post_id,’robots’,true); $ninjas = get_post_meta($post_id,’ninjas’,true); $professions = array(); if ($pirates) {$professions[] = $pirates;} if ($robots) {$professions[] = $robots;} if ($ninjas) {$professions[] = $ninjas;} if (count($professions) > 0) … Read more

Custom field as featured image caption

Here’s one way to inject the custom field as the featured image caption with help of the post_thumbnail_html filter: add_filter( ‘post_thumbnail_html’, function( $html, $post_id ) { if( $html && $caption = get_post_meta( $post_id, ‘wpcf-bildunterschrift’, true ) ) $html .= sprintf( ‘<p>%s</p>’, sanitize_text_field( $caption ) ); return $html; }, 10, 2 ); Hopefully you can adjust … Read more

Display custom field from the front page on another page

$val = get_post_meta( get_the_ID(), ‘meta_data_name’, true ); echo $val; You can replace get_the_ID() with the ID of the post you are getting the meta from. You could possible team this with: $frontpage_id = get_option(‘page_on_front’); to get the homepage ID. Not sure if this would be friendly with multisite though. the_field() is an Advanced Custom Fields … Read more