Skip Navigation

[Resolved] Check if post has layout associated for admin column in php

This support ticket is created 6 years 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by Luo Yang 6 years ago.

Assisted by: Luo Yang.

Author
Posts
#1179491

I have a custom post type products. I've made a custom admin column in php using the
manage_${post_type}_posts_columns action

For the value, I have the $post_id and I want to use that to check if that post is using a layout for it's content field. If it is using the layout plugin to design the content, I want to show a link to jump to that editor so the admin doesn't have to click the post and then click "Edit with Layouts"

#1179745

Hello,

The Layouts plugin is using a hidden custom field "_layouts_template" to store the layout slug in each post, so you can get the layout slug value with WordPress function get_post_meta(), for example:

$layout_slug = get_post_meta(get_the_ID(), '_layouts_template', true);

https://developer.wordpress.org/reference/functions/get_post_meta/

Then use slug to get layout post ID with function get_posts()
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
name (string) - use post slug.

#1179808

That works to get the overall layout for the post; however, I'm looking to see if the post is using the content layout editor and not if the post type uses a layout template.

Is that a different custom field?

#1179818

Yes, you are right, it is another custom hidden post field "_private_layouts_template_in_use", field value is "yes".

#1179822

Ok, so if "_private_layouts_template_in_use" returns "yes", then where do I get that layout ID (or slug) for the content layout editor?

I'm doing something like this in the admin column:

$layout_slug = get_post_meta($post_id, '_private_layouts_template_in_use', true);
$query = new WP_Query(array('name' => $layout_slug));
$layout_posts = $query->posts;
   if(count($layout_posts)){
      echo "<a href='/wp-admin/admin.php?page=dd_layouts_edit&layout_id=${layout_posts[0]->ID}&action=edit'>Edit Layout</a>";
   }           

Obviously this won't do anything because "_private_layouts_template_in_use" is going to give me a "yes" and not a slug or ID of the layout. Can I use this key to check if the layout is attached and another key to get the actual ID?

#1179824

The URL parameter "layout_id" value is using same value as post ID, for example:
[php]
if( get_post_meta($post_id, '_private_layouts_template_in_use', true) == 'yes' ){
echo "Edit Layout";
}
[php]

For your reference.