0
         <?php
    foreach($getPostCustom as $name=>$value) {

        echo "<strong>".$name."</strong>"."  =>  ";

        foreach($value as $nameAr=>$valueAr) {
                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                echo $nameAr."  =>  ";
                echo var_dump($valueAr);
        }

        echo "<br /><br />";

    }
?>

Actually I created a "Custom Post Type" and for that post type I added Custom Fields and Now I want to Display all my custom field values in the particular post type posts. The above Code displays all Custom Fields. Please help me to retrieve only custom fields of the particular Posts Only. Thanks In Advance..

  • 1
    Can you please elaborate your question? are you using the ACF plugins? this is a template? What is $getPostCustom? – Cesar Henrique Damascena Aug 1 '17 at 12:13
  • Actually I created a "Custom Post Type" and for that post type I added Custom Fields and Now I want to Display all my custom field values in the particular post type posts. – Pullata Praveen Aug 1 '17 at 12:25
  • These custom fields, you added them with the Advanced Custom Fields plugin? – Cesar Henrique Damascena Aug 1 '17 at 12:40
  • yes I added through Advanced Custom Field Plugins – Pullata Praveen Aug 1 '17 at 12:55
  • I edited the above code and now it works hope it may be useful for someone else please see my answer in the bottom – Pullata Praveen Aug 4 '17 at 5:13
1
    <div class="col-xs-12 col-sm-12 col-md-8 col-lg-8 left_column"> <?php
      if (have_posts()) : 
        while (have_posts()) : the_post(); ?>
        <h1> <?php the_title();?> </h1> <?php 
        $post_meta = get_post_meta(get_the_ID());
        foreach($post_meta as $key=>$value)
        { 
            echo "<strong>".$key."</strong>"."  =>  ";
            foreach($value as $nameAr=>$valueAr)
            {
                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                echo $nameAr."  =>  ".$valueAr; 
            }
            echo "<br >";       
        }
        the_content(); 
        endwhile;
        endif; ?>
      </div>
0

If you're in the single_{$post_type_slug} template . you can do this way:

// Create an array with the name of all custom field

$custom_field_names = array( 'custom_field1', 'custom_field2' );
$custom_fields;

$args = array (
 'post_type' => 'post',
 'posts_per_page' => -1
);

$posts = get_posts( $args );

// Get all the custom fields for this post

foreach( $posts as $key => $post ) {
  foreach( $custom_fields_names as $name ) {
    $custom_fields[$key][$name] = get_field( $name, $post->ID )
  }
} 
  • Thanks for the above code but i need without adding custom field names in the query @ Cesar – Pullata Praveen Aug 2 '17 at 13:22
  • Which query are you saying?, the code isn't adding any custom field to the main query, just getting the field related to the current $post. This function will run after the WP_Query. I've updated my answer. – Cesar Henrique Damascena Aug 2 '17 at 13:36
  • $custom_field_names = array( 'custom_field1', 'custom_field2' ); here are you calling custom fields? – Pullata Praveen Aug 2 '17 at 13:43
  • No, there I'm just putting the name of the custom fields that I will get. This is good because I can wall them dynamically in here: foreach( $custom_fields_names as $name ) { $custom_fields[$key][$name] = get_field( $name, $post->ID ) }. Instead of calling them one by one. Example: get_field( custom_field1, $post->ID ); get_field( custom_field2, $post->ID ); – Cesar Henrique Damascena Aug 2 '17 at 13:47
  • but I want to display all fields relagted to the posts without mentioning custom fields name – Pullata Praveen Aug 3 '17 at 3:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.