• Resolved Elliot Sowersby

    (@elliotvs)


    Hello

    I’m trying to list data from values in a custom post type.

    I can list all the titles for that post type perfectly fine, but unable to list the custom values for the post types.

    Code:

    $args = array(
        'post_type' => 'shop_coupon',
        'posts_per_page' => 100
    );
    
    $obituary_query = new WP_Query($args);
    
    while ($obituary_query->have_posts()) : $obituary_query->the_post();
    
        echo '<p>';
    
        echo '<strong>';
        the_title();
        echo '</strong>';
    
        $amount = get_post_custom_values(get_the_ID(),'amount', true);
        echo '<br/>Discount: ' . $amount[0] . '%';
    
        $usage = get_post_custom_values('usage');
        echo '<br/>Usage: ' . $usage[0];
    
        echo '</p>';
    
    endwhile;
    
    wp_reset_postdata();
    
    }

    Any ideas?

    [ No bumping please. ]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi there,

    The codex for this function:
    https://codex.www.ads-software.com/Function_Reference/get_post_custom_values

    says:

    This function is useful if you wish to access a custom field that is not unique, i.e. has more than 1 value associated with it. Otherwise, you might wish to look at get_post_meta().

    It looks to me that your fields may only contain single values so get_post_custom_values would not work.

    I would highly recommend you use get_post_meta() because it covers fields with single or multiple data in them.

    About you using get_post_custom_values:

    The syntax is:

    get_post_custom_values($key, $post_id);

    Where
    $key is the name of the custom field

    In
    $amount = get_post_custom_values(get_the_ID(),’amount’, true);

    you don’t have the name of the custom field in the first parameter, and true is not a valid parameter.

    In $usage = get_post_custom_values(‘usage’);
    echo ‘
    Usage: ‘ . $usage[0];

    You are looking for a single piece of data obviously. If the field contains a single piece of data then it outputs a single value not an array and hence $usage[0] will not exist because it would be one element of multiple data in an array.

    BUT do switch over to get_post_meta()

    Hope this helps ??

    Thread Starter Elliot Sowersby

    (@elliotvs)

    Still can’t manage to work it out, even following what you said.

    Please could you send me the changes you would suggest what you think would work?

    Much appreciated.

    Thread Starter Elliot Sowersby

    (@elliotvs)

    Hi wpfan1000

    Managed to solve the problem with the link you provided, thanks.

    Here is the outcome plugin: https://www.ads-software.com/plugins/woo-coupon-usage

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Post Type list’ is closed to new replies.