Tagged: custom post meta, get posts
- This topic has 1 reply, 2 voices, and was last updated 5 years, 11 months ago by
Steve.
-
AuthorPosts
-
-
February 15, 2016 at 7:11 pm #5932
RachelMemberSo I am basically giving users the ability to set a featured post as a drop down, and in addition to storing the ID of the post to feature, I want to include other info – like the title, excerpt, and featured image path, and have these reflect the most up to date values for the post.
Right now, I created a field template called ‘preview-post’ which displays those values to the WordPress user, but I haven’t figured out how to store the values in the DB as part of that.
Here’s my fields:
piklist('field', array( 'type' => 'group', 'label' => 'Primary Feature', 'field' => 'primary_feature', 'fields' => array( array( 'type' => 'select', 'field' => 'primary_feature_post', 'label' => 'Choose Post', 'columns' => 6, 'choices' => piklist_get_posts()), array( 'type' => 'preview-post', 'label' => 'Preview', 'columns' => 6, 'attributes' => array( 'field' => 'primary_feature', 'subfield' => 'primary_feature_post' ) ) ) ));and the field template is ‘preview-post’:
<?php array_push($attributes['class'], 'piklist-field-part'); ?> <div <?php echo piklist_form::attributes_to_string($attributes); ?> id="<?php echo piklist_form::get_field_id($arguments); ?>" name="<?php echo piklist_form::get_field_name($arguments); ?>"> <?php $post_meta = get_post_meta($post->ID, [$attributes['field']][0], false); foreach ($post_meta as $key => $val) { if (array_key_exists($key, $post_meta)) { foreach($val as $subkey => $subval) { if (array_key_exists($attributes['subfield'], $val)) { $the_post = get_post($subval); echo get_the_post_thumbnail($the_post->ID,'thumbnail') . '<h2>' . $the_post->post_title . '</h2><p>' . $the_post->post_excerpt . '</p>'; } } } } ?> </div>How do I modify my field template to save the fields to the DB?
Also, I need to be able to store the results so that they are updated in the DB should the title, excerpt, or image path be updated.
-
February 20, 2016 at 6:50 pm #5964
SteveKeymasterHi Rachel–
I think you’re overthinking this.
First, take a look at this field in the Demos: Add-mores > two level > “Content Section (Grouped)”. It shows you how to pull and save Post IDs. Your code does not need to be in a “group”, so just display it in a normal select field.
Once you save, and pull, the Post ID, you can then use the standard WordPress function get_post() to get the information you need.
-
-
AuthorPosts
- You must be logged in to reply to this topic.