• I have a custom field “cfs_writer” which holds the value of a custom post ID. It can have 0 or more values. My post custom fields metabox shows:

    • cfs_writer value=43
    • cfs_writer value=55

    I can display the first value without a problem:
    [field cfs_writer] which displays: 43

    But I can’t find a way to display more cfs_writer values (if they exist). I’ve tried various forms of “list”, “fields” and “array”. The field seems to be a single field with multiple values, and hence a “list”, rather than multiple fields with the same name. Any suggestions?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter iantresman

    (@iantresman)

    Using a fresh WordPress install, I’ve added to a post, a custom field called “Colour” and assigned it a value “Red”, and another with a value of “Green” (screenshot).

    I can display the first value using [field Colour] but don’t know how to access the second value “Green”. I’ve tried the following:

    1. [array Colour] (and with each=true)
    2. [for each=Colour]

    @iantresman

    personally I’d use an ACF repeater. I’m not sure CCS is set up to process this.

    looking at this it would need a way to pass single=false to get_post_meta and return an array maybe: https://developer.www.ads-software.com/reference/functions/get_post_meta/

    (Would be great if we could use [pass array=some_field] and have it assume therefore some_field needs to be returned as the array)

    the code below isn’t future proof but gives you some option until the developer updates the plugin:

    in the plugin include/core/content.php line 1927, you can try set this to false instead of true at the end
    $result = get_post_meta($post_id, $field, false);

    This returns the field as a comma delimited string though not an array (not sure why), so the [pass array...] functions don’t work and I’m not sure what knock on effects making this change would have either

    But you could then use string functions though to get values

    Colour 1: [format split="," part=1][field colour][/format]
    Colour 2: [format split="," part=2][field colour][/format]

    or..

    [pass field=colour]
    Colour 1: [format split="," part=1]{COLOUR}[/format]
    Colour 2: [format split="," part=2]{COLOUR}[/format]
    [/pass]

    but it’s kind of messy solution

    it also gives you access to a pass loop

    pass list check:

    [set temp][field colour][/set]
    [pass vars]
    [-pass list='{TEMP}']
    {-ITEM}
    [/-pass]
    [/pass]

    or more simply…

    [pass field=colour]
    [-pass list='{FIELD}'] // creates a list from the comma separated values
    {-ITEM}
    [/-pass]
    [/pass]

    hope that’s of use

    It would be great if @miyarakira could look into this.

    with the above change to content.php to allow for field arrays, also it seems in includes/modules/pass.php that the code below is what breaks the [pass array...] functions

    ($field_value is being returned as a string not an array with the slower version)

    // Predefined or custom field
    } else {
       // $field_value = CCS_Content::get_prepared_field( $field, $post_id );
       // Slower but will get date format, etc.
       $field_value = CCS_Content::content_shortcode($atts);

    if you swap those comments around so it uses the get_prepared_field version, this code now works

    [pass array=colour]
    Colour 1: {0}, 
    Colour 2: {1}
    [/pass]

    (Is it possible to get the length?)
    (again it needs the previous modification for get_post_meta to return an array)

    @miyarakira another potential extra change:

    on line 2501 of includes/core/content.php

    } else {
      $id = do_shortcode('[field id]');
      $array = get_post_meta( $id, $field, true ); // <== change to false 
    }

    this then returns the field as an array as expected so this now works:

    [array field=colour]
    [field value]
    [/array]
    Plugin Author Eliot Akira

    (@miyarakira)

    Hi @iantresman – I’m working with @codemonkeynorth to implement a solution. Will let you know when it’s ready.

    • This reply was modified 4 years, 5 months ago by Eliot Akira.
    Thread Starter iantresman

    (@iantresman)

    Thank you very much, appreciated.

    Hello iantresman,
    could You try this code:

    [array cfs_writer each=true]
    [field value]
    [/array]

    .. or..

    [array Colour each=true]
    [field value]
    [/array]

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Accessing multiple value from a custom field’ is closed to new replies.