Support

Account

Home Forums General Issues Generate sub field from php – check if content already exists

Solving

Generate sub field from php – check if content already exists

  • I have an issue which I need to get around.
    I want to generate pages and custom fields from php. I have a repeater field, and I’m trying to add a row with some text in one of the fields.

    I use this code:

    $field_key = "pagetype1_sidebar_items";
    $value = get_field($field_key, $page_id);
    $value[] = array( "pagetype1_sidebar_item_header" => "The row was created along with this text" );
    update_field( $field_key, $value, $page_id );

    So that’s how you can specify a sub field. But this will always add new rows, not update existing ones I guess.

    But when I do this, not only one row is added, but two or sometimes more (between 2-6). A similar thing happens when adding a page – instead of one at a time, there are two or more pages added. This code is executed in functions.php with this hook:
    add_action( 'after_setup_theme', 'add_pages' );
    , so it is executed with each page load.

    I control the creating of the page by checking if the page title is unique, then only one is added.

    But in the case of a custom sub field, I don’t know how to check if the content already exists. I tried:
    $subfield = get_sub_field('pagetype1_sidebar_item_header');
    I thought this would give me an array with the values of all the rows.
    But var_dump($subfield); only gives me ‘false’. And print_r($subfield);doesn’t give anything.

    Otherwise I would loop through it and check each value. How can I do it?

    There is another way of updating individual rows, but I can’t create new rows with this method:

            $header = 'pagetype1_sidebar_items_4_pagetype1_sidebar_item_header';
            update_post_meta($page_id, $header, "The header text was updated");

    (That updates row number five.)

    But I need to work with the first code and check if the row content already exists somehow – hope you have an idea.

  • If this is something you only want to do once when the theme is activated maybe you should be using after_switch_theme instead of after_theme_setup

    https://codex.wordpress.org/Plugin_API/Action_Reference/after_switch_theme

  • I’m just testing things right now to learn how to create all the custom fields. So while I’m doing this, it is pretty convenient to have it run on each page load. I just need to prevent it from creating duplicate content when running it several times.

  • It this is the case then I would probably add an option when I did it the first time https://codex.wordpress.org/Function_Reference/add_option and check this option to see if it’s already been run. Might be the simplest thing to do.

    
    $option = get_option('my_option');
    if (!$option) {
      // run setup
      add_option('my_option', true);
    }
    
  • Could be interesting. But I still want to be able to check how many rows there are in a repetaer field and also check the values of the fields.

    In my first post I used the wrong function (get_sub_field), and I checked a sub field.

    if I use get_field_object on a repetaer field (which holds rows with sub fields):
    $subfield = get_field_object('pagetype1_sidebar_items');

    I get this repeater field object. But I thought it would contain the rows with the sub fields? If I var dump it I get:

    array(18) {
      ["key"]=>
      string(29) "field_pagetype1_sidebar_items"
      ["label"]=>
      string(0) ""
      ["name"]=>
      string(23) "pagetype1_sidebar_items"
      ["_name"]=>
      string(23) "pagetype1_sidebar_items"
      ["type"]=>
      string(4) "text"
      ["order_no"]=>
      int(1)
      ["instructions"]=>
      string(0) ""
      ["required"]=>
      int(0)
      ["id"]=>
      string(33) "acf-field-pagetype1_sidebar_items"
      ["class"]=>
      string(4) "text"
      ["conditional_logic"]=>
      array(3) {
        ["status"]=>
        int(0)
        ["allorany"]=>
        string(3) "all"
        ["rules"]=>
        int(0)
      }
      ["default_value"]=>
      string(0) ""
      ["formatting"]=>
      string(4) "html"
      ["maxlength"]=>
      string(0) ""
      ["placeholder"]=>
      string(0) ""
      ["prepend"]=>
      string(0) ""
      ["append"]=>
      string(0) ""
      ["value"]=>
      bool(false)
    }

    It doesn’t contain the two rows with sub fields. So this doesn’t get me the info I need – I need to check if there are rows, and if so, check the values of the rows.

  • The field object contains the field definition used by ACF.

    If you just want to check the number of rows in a repeater the simplest way to do that is to use $count = intval(get_post_mets($post_id, 'repeater_field', true)); The values stored by ACF in the post_meta table for the repeater is the number of rows it contains.

    If you want to check the actual values in those rows then you need to use the ACF while(have_rows()) { the_row; get_sub_field('subfield');} code used for repeater fields.

    There are some cases that get_field('repeater') will actually return an array with the content of sub fields, but that is not something that you can count on. There are other times when it just returns the number of rows as in the get_post_meta() code above.

  • Great, thanks! But it didn’t register any rows when doing “have_rows”!
    I’m checking a repeater field that has 2 rows.

    
            $field_key = "pagetype1_sidebar_items";
    
            if( have_rows($field_key) ):
                echo "<p style='margin-left:180px;'><br>there are rows</p>";
                while ( have_rows($field_key) ) : the_row();
    
                $header = get_sub_field('pagetype1_sidebar_item_header');
                echo "<p style='margin-left:180px;'><br>subfield:. " . $header . "</p>";
    
                endwhile;
            else :
                echo "<p style='margin-left:180px;'><br> No rows</p>";
            endif;

    This gives me “No rows”.

    By the way, I didn’t find any documentation on the “the_row()” function, is it explained somewhere?

  • The only place that I know of the_row() being mentioned is on the have_rows() page http://www.advancedcustomfields.com/resources/have_rows/

    The only reason that this would not return any values is if you are not inside “The Loop”. If you are outside of the main loop then you need to add the post ID to get the values from.

  • So there is no way to get sub field values without doing the “have_rows()” loop which also have to be nested inside the wordpress loop?

    This didn’t work for me though. This is running from functions.php:

    
    if ( have_posts() ) :
      while ( have_posts() ) : the_post();
    
        if( have_rows($field_key) ):
          echo "<p style='margin-left:180px;'><br>rows finns</p>";
              while ( have_rows($field_key) ) : the_row();
    
                  $header = get_sub_field('pagetype1_sidebar_item_header');
                  echo "<p style='margin-left:180px;'><br>the sub field value is: " . $header . "</p>";
    
              endwhile;
        else :
                echo "<p style='margin-left:180px;'><br> no rows</p>";
        endif;
     endwhile;
    endif;
  • You are trying to get the values from the current post? or are you trying to get the values from some specific post?

    If it’s from the current post, the reason that the above isn’t working is probably because it’s in a function? and inside a function the values used to create a post loop don’t exist.

    Try adding this to the top of your function

    
    global $wp_query, $post;
    
Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Generate sub field from php – check if content already exists’ is closed to new replies.