• 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: [Resolved]Checking for value of a repeating field seems not to work

Support Home » Topics » Piklist » Checking for value of a repeating field seems not to work

Tagged: get_post_meta, piklist::pre

  • This topic has 7 replies, 2 voices, and was last updated 6 years, 4 months ago by Steve.
Viewing 7 reply threads
  • Author
    Posts
    • September 13, 2015 at 5:33 pm #4317
      cosmocanuck
      Member

      Hi guys!

      I’m trying to prevent displaying a field if it’s empty – but for one repeating field, my standard approach is not working:

      <?php 
      $ticket_links = get_post_meta($post->ID, 'ticket_links', true);				
      if (!empty($ticket_links[0])  ) {				
      piklist('buy-tickets-template', array('data' => $ticket_links, 'loop' => 'data')); 							
      }
      ?>

      If I leave the “if” statement out, it runs fine, but then, even if I’ve deleted all repeating fields but the first one, and deleted all text in that first repeating field… I get something displayed (my template includes some code to make a button out of elements in the repeating field, and I get a sort of half-button even if my sole repeating field is empty.)

      This is only if I’ve used the repeating field(s) but then want to “empty them out”. Is there a proper way to do that? Or to check first with a better “if” statement before displaying?

      Thanks!
      adam

    • September 13, 2015 at 10:10 pm #4318
      Steve
      Keymaster

      @cosmocanuck– Can you post your field code for ticket_links?

    • September 13, 2015 at 10:53 pm #4319
      cosmocanuck
      Member

      Here you go!

        piklist('field', array(
          'type' => 'group'
          ,'field' => 'ticket_links'
          ,'label' => __('Ticket Links') 
          ,'columns' => 12
          ,'add_more' => true
          ,'fields' => array(
            array(
              'type' => 'datepicker'
              ,'field' => 'screeningdate'
       		,'label' => 'Screening Date'
              ,'columns' => 3
              ,'options' => array(
         			'dateFormat' => 'M d, yy'
      			)
            )
            ,array(
              'type' => 'text'
              ,'field' => 'screeningURL'
       		,'label' => 'Full ticket URL'
              ,'columns' => 9
              
            )   
          )
        ));
      
    • September 15, 2015 at 11:52 am #4325
      Steve
      Keymaster

      @cosmocanuck— What version of Piklist are you using?

    • September 15, 2015 at 12:01 pm #4330
      cosmocanuck
      Member

      0.9.4.28.

    • September 15, 2015 at 12:17 pm #4333
      Steve
      Keymaster

      get_post_meta has a third parameter, which you set to true. From the codex page for get_post_meta:
      Whether to return a single value.

      However, in your code you are looking for an array key, not a single value: $ticket_links[0], so you should set that parameter to false:
      $ticket_links = get_post_meta($post->ID, 'ticket_links', false);

      The best way to debug this in the future is to look at your data. PHP has a function, print_r, that helps you do this. However, print_r doesn’t always display values in the easiest to read format, so Piklist has a function to help: piklist::pre(). You can use either.

      So, to debug in the future do something like this:

      $ticket_links = get_post_meta($post->ID, 'ticket_links', true);
      piklist::pre($ticket_links);
      

      You will see that there is no [0] key for you to check.

      Now, change the third parameter to false:

      $ticket_links = get_post_meta($post->ID, 'ticket_links', false);
      piklist::pre($ticket_links);
      

      WordPress now added that key for you.

      Hope that helps, and makes sense.

    • September 15, 2015 at 12:27 pm #4335
      cosmocanuck
      Member

      Thanks Steve, that looks like exactly what I need – now to work through it… slowly! 8^)

    • September 15, 2015 at 12:33 pm #4337
      Steve
      Keymaster

      Great! Closing ticket.

  • Author
    Posts
Viewing 7 reply threads
  • The topic ‘Checking for value of a repeating field seems not to work’ is closed to new replies.

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.