8

I have several post types. I want to get all custom fields associated with that post type.

Example:

Post
-- image
-- Featured image
-- body

I need to get all fields or custom fields in array. I found a solution from here, but it does not serve my purpose:

    echo '<pre>';
    print_r(get_post_custom($post_id));
    echo '</pre>';
8
  • I need all fields with a post type. it may be feature image or body, custom field. Not for post. :) ex: array( 'post_type'=>array('image,body,title,featured_image')) post type may be page or post or anything. i hope clear now.
    – user115277
    Commented Mar 17, 2017 at 15:43
  • Have you a piece of code, again? A piece to show us. Here is nobody to write a code for you.
    – Max Yudin
    Commented Mar 17, 2017 at 17:17
  • i am not coding any thing. i need idea. i am not telling any body to code for me. I search lots of time to find the solutions. Now i am searching in wordpress core how to they retrieve field with other custom fields in a post or page. If i found i will answer it here. Thanks
    – user115277
    Commented Mar 17, 2017 at 17:33
  • Please post your code to find mistakes in. Otherways I'll delete your post as the fail to meet the requirements of this site.
    – Max Yudin
    Commented Mar 17, 2017 at 17:47
  • Ok You can. you are not able to understand what I want... :)
    – user115277
    Commented Mar 17, 2017 at 18:28

2 Answers 2

2

Add the following code in the functions.php

for acf

function get_all_meta($type){
              global $wpdb;
              $result = $wpdb->get_results($wpdb->prepare(
                  "SELECT post_id,meta_key,meta_value FROM wp_posts,wp_postmeta WHERE post_type = %s
                    AND wp_posts.ID = wp_postmeta.post_id", $type
              ), ARRAY_A);
               return $result;
          }

function acf(){
    $options = array();
    $acf = get_all_meta('acf');
    foreach($acf as $key => $value){
      $options['post_type'][$value['post_id']]['post_id'] = $value['post_id'];
      $test = substr($value['meta_key'], 0, 6);
      if($test === 'field_'){
        $post_types = maybe_unserialize( $value['meta_value'] );
        $options['post_type'][$value['post_id']]['key'][] = $post_types['key'];
        $options['post_type'][$value['post_id']]['key'][] = $post_types['name'];
        $options['post_type'][$value['post_id']]['key'][] = $post_types['type'];
      }
      if($value['meta_key'] == 'rule'){
        $post_types = maybe_unserialize( $value['meta_value'] );

        $options['post_type'][$value['post_id']]['post_types'] = $post_types['value'];
      }
    }
    return $options;
  }

This will give you the array value of post meta key for acf.

How to use

foreach(acf() as $key => $value){
update_post_meta(76, $value['type'], 'Steve');
}

For pods

function pods(){ global $wpdb; //get the pods post types id.

    $result = $wpdb->get_results($wpdb->prepare(
      "SELECT ID,post_title,post_parent FROM wp_posts WHERE post_type = %s", $type
    ), ARRAY_A);

// pods each field for a post type create separate post type so again query to get the field post type result.
    $pods_field_post_type = array();    
    foreach($result as $value){
    $pods_field_post_type = $wpdb->get_results($wpdb->prepare(
    "SELECT ID,post_title,post_name FROM wp_posts WHERE post_type = %s
    AND post_parent = %d
    ", '_pods_field',$value["ID"]
    ), ARRAY_A);

    }
$fields = array();
   foreach($pods_field_post_type as key => $value):
     $podsAPI = new PodsAPI();
     $pod = $podsAPI->load_pod( array( 'name' => '
  post' ));
     $fields[] = $pod['fields'][$value['post_name']]['type'];
  endforeach;
  }
print_r($fields);

How to use

foreach($fields as $key => $value){
update_post_meta(76, $value, 'Steve');
}

function acf() and pods() give you the exact meta key of that post type. If you copy and paste the code it may not work.

Hope this answer help you and also others.

let me know you not trouble anything.

7

Post Type Features

Several options exist to obtain a list of "features" which a particular post type supports. The easiest is to simply use the get_all_post_type_supports() function:

$post_type_features = get_all_post_type_supports( 'post' );

print_r( $post_type_features );
/* Array
 * (
 *   [0] => author
 *   [1] => editor
 *   [2] => thumbnail
 *   [3] => excerpt
 *   [4] => trackbacks
 *   [5] => custom-fields
 *   [6] => comments
 *   [7] => revisions
 *   [8] => post-formats
 *   [9] => title
 * )
 */

You can also use post_type_supports() to see if a post type supports specific features.

Another way to check a post type's features it to obtain it's post type object and simply check the $supports property:

$post_type_object = get_post_type_object( 'post' );

print_r( $post_type_object->supports );

Custom Fields

Since custom fields are post meta-data associated on a post-by-post basis rather than a post-type, there is no direct way to retrieve a list of custom field names used by a particular post type.

Rather, you would have to compile an array yourself by looping through every post of the post type in question and using get_metadata( $post_id ) or get_post_custom_keys( $post_id ) to retrieve every custom field stored for that post. This is potentially a very expensive operation and should be done as infrequently as possible - if this is something you are considering doing more than once every week or month, I'd highly recommend that you look into other means to accomplish your end-goal.

A custom SQL query to determine this information is also possible, and would no doubt be significantly more performant than querying meta-data for every post of a post type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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