• Documentation
  • Support / Community
  • Products
    • Piklist Framework
    • Toolbox
    • Fields and Forms
    • WordPress Helpers
  • Log In
Piklist

Access your account, and post on the Support Forums

Retrieve Password | Register for account

Topic: Setting additional meta fields based on select box

Support Home » Topics » Piklist » Setting additional meta fields based on select box

Tagged: custom post meta, get posts

  • This topic has 1 reply, 2 voices, and was last updated 5 years, 11 months ago by Steve.
Viewing 1 reply thread
  • Author
    Posts
    • February 15, 2016 at 7:11 pm #5932
      Rachel
      Member

      So 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
      Steve
      Keymaster

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

  • Author
    Posts
Viewing 1 reply thread
  • You must be logged in to reply to this topic.
Log In

Log in / Register

Register for an Account

Stay up-to-date with Piklist

About

  • Philosophy
  • Privacy Policy
  • Terms of Service
  • Contact Us
  • Register for an account

Resources

  • Community Forums
  • Documentation
  • Tutorials
  • Get involved

Follow Piklist

  • Like us on Facebook
  • Follow us on Twitter
  • Connect on Google +
  • Watch on YouTube

Rate Piklist


If you love Piklist leave us a 5 Star review on WordPress.org.

News

  • Piklist v0.9.9.8 is now available

    September 30, 2019

    In October of 2015, we released v0.9.9 of Piklist. Your help in testing lead to seven(7) m ...

  • Piklist is now on HackerOne

    September 30, 2019

    We built Piklist from the ground up with security as our top priority. While we’ve t ...

  • Read all News
  • Powered by Piklist & WordPress © 2009 – 2022 Piklist LLC.
    This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to the use of cookies. Find out more.