Support

Account

Home Forums General Issues Retrieve fields for post type in home page

Solving

Retrieve fields for post type in home page

  • Hi

    I have CPT Call product with 4 custom fields
    I want to reterive field [pref] in home page with the latest product listed in the post type.

    Can anyone help me ?

  • May this is, what you are looking for. First, the get_posts() function gets the posts from the post type “product” ordered by date with the latest date first and returning only the first result by ‘numberposts’ => 1. Then get the field value of that post using its ID.

    $latest_post = get_posts(array(
        'numberposts' => 1, // restrict result to only the latest post
        'post_status' => 'publish',
        'post_type' => 'product',
        'orderby' => 'date',
        'order' => 'DESC'
    ));
    
    $prev = get_field( 'pref', $latest_post[0]->ID );
  • Thanks johaann,
    It’s work as <?php the_field(‘prefs’); ?> !

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Retrieve fields for post type in home page’ is closed to new replies.