Skip Navigation

[Gelöst] Get custom fields associated with custom post type

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created vor 8 Jahre, 11 Monate. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 3 Antworten, has 3 Stimmen.

Last updated by shanep-3 vor 8 Jahre, 11 Monate.

Assisted by: Beda.

Author
Artikel
#302172

I am trying to: Retrieve all of the custom field names (keys) associated with a custom post type. I haven't found any WP documentation on this; It appears that WP does not associate custom fields with custom post types. But it looks like Types does make that association. Thanks for any thoughts!

I visited this URL:

I expected to see:

Instead, I got:

#302353

Thank you for contacting us here in the Support Forum

I am not sure for what you will retrieve all the Custom Fields assigned to a certain Custom Post Type, or how you would like to use them afterwards.

To help you more effectively, I would need to know the exact goal you try to achieve, and how you plan to use Toolset Plugins for this.

Either way, I found some Documentation that might help you achieving your goal:
Get from a specific post:
https://codex.wordpress.org/Function_Reference/get_post_custom
https://codex.wordpress.org/Function_Reference/get_post_meta
https://codex.wordpress.org/Function_Reference/get_post_custom_keys

Get for all Posts of a certain Post Type:
http://wordpress.stackexchange.com/questions/9394/getting-all-values-for-a-custom-field-key-cross-post

Please let me know if you have further questions regarding the issue mentioned in this Thread
and let me know if some of the above solutions works for you, I look forward to your reply!

Thank you

#304283

Thanks for the links to the WP codex but they don’t address my question, which was about accessing Types data.

I have found my own solutions which I will share in the hope that it will assist someone else.
Why would I want to retrieve all of the custom fields associated with a custom post type? In short, to control visibility.

My client wanted users to be able to submit information through a front-end form (not the admin dashboard). There are five different custom post types and two dozen custom fields, some of which are common between the post types (like name, email, etc) and some of which are unique to the post type. The idea was to show the common fields and, using a drop-down, select the appropriate custom post type and display those fields.

Here is the function I wrote; it performs several queries on the WP DB, pulling the info stored by the Types plugin. Given a custom post type, the function will return an array of the custom fields plus all meta stored with those fields, including name, slug, description, placeholder, required and validation. It allows me to create a dynamic form that is completely managed by the client from the Types interface.

function get_custom_fields_by_post_type($post_type) {
	global $wpdb;
	$query = "
		SELECT * 
		FROM  $wpdb->postmeta AS pm1, $wpdb->postmeta AS pm2, $wpdb->posts
		WHERE pm1.meta_key = '_wp_types_group_post_types'
		AND pm1.meta_value LIKE '%$post_type%'
		AND pm2.post_id = pm1.post_id
		AND pm2.meta_key = '_wp_types_group_fields'
		AND $wpdb->posts.ID = pm2.post_id
		ORDER BY $wpdb->posts.post_title ASC
	";

	$results = $wpdb->get_results ( $query );
	$cf_meta = get_custom_field_meta();
	$i=0;
	$my_cfs[post_type] = $post_type;
	foreach($results as $result) {
		$my_cfs[data][$i][group_name] = $result->post_title;
		$my_cfs[data][$i][group_slug] = $result->post_name;
		$the_fields = explode(',',$result->meta_value); // custom fields stored as csv string, but with commas at front and back
		$the_fields = array_filter($the_fields); // deletes empty array elements
		$x=0;
		foreach($the_fields as $the_field) {
			$my_cfs[data][$i][fields][$x] = $cf_meta[$the_field];
			$x++;
		}
		$i++;
	}
	return $my_cfs;
}

This function returns the same data but queries on Types Custom Field Group instead of custom post type:

function get_custom_fields_by_group($group_slug) {
	global $wpdb;
	$query = "
		SELECT * 
		FROM $wpdb->posts,$wpdb->postmeta
		WHERE $wpdb->posts.post_name LIKE '%$group_slug%'
		AND $wpdb->postmeta.post_id = $wpdb->posts.ID
		AND $wpdb->postmeta.meta_key like '_wp_types_group_fields'
	";
	$results = $wpdb->get_results ( $query );
	$my_cfs = (array)$results[0]; // converts object to array
	$cf_meta = get_custom_field_meta();
	$the_fields = explode(',',$results[0]->meta_value); // custom fields stored as csv string, but with commas at front and back
	$the_fields = array_filter($the_fields); // deletes empty array elements
	$x=0;
	foreach($the_fields as $the_field) {
		$my_cfs[fields][$x] = $cf_meta[$the_field];
		$x++;
	}
	return $my_cfs;
}

Both functions rely on this helper function. Types stores all of the custom field relational data in one big serialized array, meaning you can’t query it directly. Instead, we just retrieve the info, unserialize it, and give it to the other functions.

function get_custom_field_meta() {
	global $wpdb;
	$cf_meta = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = 'wpcf-fields'");
	$cf_meta = unserialize($cf_meta->option_value);
	return $cf_meta;
}
#564952

thank you so much! this is perfect! how did you find this solution?

Das Forum „Types Community Support“ ist für neue Themen und Antworten geschlossen.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.