Skip Navigation

[Resolved] get_posts doesn't get types custom fields

This support ticket is created 7 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 7 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#405152

Hi

This is a follow on from the previous support question you answered "Post relationships for imported data", where the imported data included the relationships between parent "courses" and child "modules".

Following on from that I have imported the module id's into a custom taxonomy of the parent course.

I am now pulling the module data out of the db using the php templates. Here's the code I'm using:

$taxonomy = 'coursemodule'; 
$terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "names" ) );
if( $terms ) {
  foreach ($terms as $module) {
	  
	  $args = array(
		'posts_per_page'   => 1,
		'orderby'          => 'post_title',
		'meta_key'   => 'wpcf-coursecalid',
		'meta_value' => $module,
		'post_type'  => 'module'
 	  );
	  $postslist = get_posts( $args );
    	foreach ( (array) $postslist as $post ) {
      	  $ID = $post->ID;
          $title = $post->post_title;
          $alias = $post->alias;
          $content = $post->post_content;
		  if (is_numeric($ID)) {
            echo $title . "<br />";
            echo $alias . "<br />";
            echo $content . "<br />";
	  		echo "<br />";
		  }
      }
	//  print_r($postslist);
 
  }  //end foreach
}

So for each taxonomy I'm using get_posts to pull in the "module" posts with wpcf-coursecalid matching the taxonomy.
This works up to a point, but none of the custom fields I've added to the modules post type are pulled into the $postlist array by get_posts.

Is there a custom 'Toolset' version of get_posts that will pull in the custom fields as well? I seem to remember there being a lot more documentation about using php templates a year or two ago.

Failing that, is there a way I can do this within the "Content Templates" within Toolset instead? I can't see how it would work but you never know.

Thanks
Tim

#405159

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Tim

Looking at your code I'm confused by what you are trying to achieve. Not knowing your data, what your terms are etc., it's a bit hypothetical.

Can you describe in words what you are aiming to do?

Right now you appear to be retrieving the terms for the coursemodule taxonomy attached to a specific post, then looping through each of those terms to find module posts with the custom field coursecalid that matches the term, and the printing post data.

If I can understand what you are aiming for I might be able to propose a Toolset solution that doesn't require writing custom PHP, or, at least, give your code the once over.

As far as retrieving the meta data with your posts, there is no all-in-one function I know of, I'm not aware of any alternative to first retrieving the post and then retrieving the meta data for that post.

#405167

Hi Nigel

It is a bit of a weird one. I'll try to explain a bit more clearly.

I'm importing data from XML files. The XML files already contain relationships between the courses and modules (and between the modules/trainers and modules/venues, but that's for later).

From my previous support ticket you replied to: (https://toolset.com/forums/topic/post-relationships-for-imported-data/) it was clear I can't import that relational data from the XML directly into a Toolset parent/child relationship as they work on post_id's and until the data is imported the post_id doesn't exist.

So this attempted solution is pretty much as you interpreted it. I have imported the module id's from XML into a custom taxonomy "coursemodule", so each course might have 0, 1 or more coursemodule values.

In this code (from single-coursedate.php) I am retrieving the terms for the coursemodule taxonomy attached to a specific post. The contents of the taxonomies' "name" fields are number that relate to the child "module" coursecalid (held in a custom field).

From there I'm looping through the taxonomies that relate to that course and selecting the 'module' post that has:
'meta_key' => 'wpcf-coursecalid',
'meta_value' => $module,
where $module is the module id retrieved from the taxonomy.

So now I've created a relationship between the course and it's child modules via the taxonomy. I can use this to display the title and content (and other standard WP fields).

The problem is now I want to display the module details (Date, venue, tutor, price) for each module, but these are all held in Toolset custom fields and aren't retrieved by get_posts.

I hope that explains it a bit clearer. It may be that there is a more sensible way to do this using the Toolset layouts but I can't see how.

Thanks for your help.
Tim

#405348

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, I don't think Toolset is going to help you with that, and you seem to have done all the leg work with your custom function in any case.

To retrieve the meta data for your module posts, is there some reason you cannot/don't want to use get_post_meta? (See: https://developer.wordpress.org/reference/functions/get_post_meta/)

You have the post id for the current module, and so you call get_post_meta to retrieve the required fields (which are stored in the db with the wpcf_ prefix, e.g. as 'wpcf_tutor').

#405349

Hi Nigel

Yes, I've got to get_post_meta eventually and have managed to retrieve all I need.

Just thought there might be a function that would retrieve all types custom fields from a post at the same time in one query instead of querying for each field.

Thanks for your help and sorry for the long, convoluted explanations.

Cheers
Tim

#405472

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Tim,

While checking something relating to another question it occurred to me you might find helpful something like types_get_fields_by_group(), which you can use to retrieve all of the metadata for a group of fields at once.

Not documented and not supported, but you can look at the source code in wp-content/plugins/types/library/toolset/types/embedded/includes/api.php, I think you should be able to follow and work out what the code is doing.

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