Get ACF from relationship field with Timber - php

I'm using Advanced Custom Fields on a custom post type. On the homepage I have a relationship field to select one of the posts in the custom post type.
Using Timber, I'm struggling to get the ACF fields for that relationship post into the Context. I can get the standard wordpress info like title and content, but not the ACFs.
I think I need to use TimberPost, but haven't had any success so far.
This is in my page template. In the view it just outputs all the standard WP stuff:
$context['featureRelationship'] = get_field('feature');
So I tried this, but I'm going wrong somewhere:
$context['featureRelationship'] = new TimberPost(get_field('feature'));
Appreciate any help, thanks

Sorted, so the php was right:
$context['featureRelationship'] = new TimberPost(get_field('feature'));
But I should have used a post object acf field instead of relationship

Related

Custom WordPress permalink structure

Is it possible to have the following structure, and if so, how:
www.example.com/something (A custom archive page with posts from a certain post category ['poca'] and a certain custom post type ['cuty'])
www.example.com/something/beautiful (A post of category ['poca'])
www.example.com/something/ugly (A post of my custom post type ['cuty'])
I can achieve to get my custom archive page to work. Moreover everything works so that 'poca' and 'cuty' takes the same template. But coming from something to one of these posts, it takes the normal permalink (category-name or custom-post-type) instead of something
Is there a way to achieve that it takes my custom archive page into the url instead?
By the way: I am able to achieve the result by using a custom shortcode for my custom post type and then creating a new post with that shortcode in the wanted category, but this brings a consistency problem, as there's always the need to create two posts for the one thing I'd like to have.
So I finally found a solution. Seems not the best way but it's ok for me to do it this way...
Through using the plugin "Custom Permalinks" I can add the permalink I want, so I only need to add something for all my new posts in that category and all new posts of my custom post type. But I am still searching for a solution which does that part automatically if somebody knows...

Wordpress: I want to get the name of a meta field

I am using the Post Type Builder for Wordpress to create Custom Post Types.
I would like to get the name of a meta field in the front-end. I am able to get the values, but I would like the names. How could I do this?
For example, I have made a custom post type for a machine selling website.
The user is able to add a new machine (post type), add the building year, dimensions, weight and so (meta data).
I would like to display: Year: 2001. How can I get "Year"?
Thank you in advance!
The best way would be to use the Post Type Builder template editor (https://themify.me/docs/post-type-builder-plugin-documentation#templates).
If not that, then the simple answer is that you already know what the name of the field is since you're passing the key in. You can just hard-code that into your page template.
The final method would be to unserialze and parse out the ptb_plugin_options item from the wp_options table. This is one big blob that holds all of the config info for the plugin. You would retrieve it something like:
$ptb_plugin_options = unserialize(get_option('ptb_plugin_options'));
echo $ptb_plugin_options['cpt']['POSTTYPE']['meta_boxes']['text_1']['name'];
Replacing POSTTYPE with the actual posttype you're using and text_1 with the meta_key of the box in Post Type Builder.

Use custom post type or custom group within another one

I am working on a wordpress website and I am using Advanced Custom Fields and Custom post type UI plugins. What I want to do is that:
The website I am working on was in native php and I want to migrate from native to wordpress but I face this problem, I can't achieve the relations that I had in mysql, in the form of post type and custom field groups in wordpress and ACF.
SO any have an Idea how to do this.
I also checked reusable fields but It actually won't help
https://github.com/tybruffy/ACF-Reusable-Field-Group
You didn't post your Data Model so it's hard to tell what the issues are or to give you an example based on it.
However, regarding the way Wordpress handles custom post type and meta value, I suggest that you consider post types as entities while meta values (custom fields here as you used ACF) usually their properties.
Usually, modelling 1-M or M-M relationships can be done in Wordpress by introducing custom taxonomies normally solve the problem, since it works as a mechanism to classify, to categorize and to group your entities. An example of this is the way Woocommerce give options to treat their product attributes as custom taxonomies. You can view the code from the link below.
https://docs.woocommerce.com/wc-apidocs/source-class-WC_Product.html#1398-1417

Wordpress: Register existing custom post type?

I've been struggling on using the same categories used on regular posts on custom posts, so I can query custom posts based on categories too.
Thing is, the custom posts weren't created using register_post_type at all, they are created by just using wp_insert_post with a different post type, forcingly creating a new type that doesn't support categories at all, so querying with categories has no use. The page is live, so there are a few important custom posts in the database already, without support for categories.
So, if I made a call to register_post_type in init now (to add categories by adding the 'taxonomies' => array('category') parameter, would new custom posts be queriable, without destroying the old, existing posts in the database? Or should I try something else entirely?
I've thought about using meta data for the custom posts, but I want to keep integrity between regular and custom's categories.
Thanks for the attention.
register_post_type is mostly used so the post type is displayed in your wp-admin. It will not affect your current database records.

Sort the admin area of a Wordpress custom post type by a custom field

It was easy enough to get my custom fields to show up as columns in the wp-admin area of my custom post type but now I need to sort by one of those custom fields. Any ideas?
But of course. This will sort posts by the value of the pant_size custom field:
$posts = query_posts('orderby=meta_value&meta_key=pant_size&order=DESC');
You can read more about it in this article.
Hopefully the answer to this will end up on the Wordpress stackexchange site.

Categories