Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom post types need to share a common post meta field WordPress

  • SOLVED

UPDATE:

Here is a better explanation of what I am wanting to accomplish:

There are 3 custom post types: A, B, and C

Each post type has individual posts. Each post type has a many-to-many relationship with one another. Each post type has custom meta fields that list all the posts of the other post types with checkboxes. So if CPT A has 5 posts, CPT B and CPT C will both have metaboxes showing all 5 posts from CPT A with checkboxes and their name.

Let's say that CPT B already has three posts: 'One', 'Two', and 'Three'. Let's also say that CPT C already has two posts: 'Fun' and 'More Fun'. Now in CPT A, I create a post called "Value". In the metaboxes below for CPT B and CPT C, I select 'One' for CPT B and 'More Fun' for CPT C. What I want is that when I go to either 'One' from CPT B or 'More Fun' from CPT C, the metabox that lists all the posts from CPT A should have 'Value' checked. That's the type of relation/sharing that I am looking for. I hope this clears things up.

Ok, so I know how to have the same post meta field displayed across custom post types, but what I need is that when one person updates the post meta field on post type "a", the field is also updated to reflect the new data in post type "b" and post type "c". How can I link these?

Answers (4)

2011-11-14

John Cotton answers:

Thomas

It sounds like you should be storing in as an option field:

[[LINK href="http://codex.wordpress.org/Function_Reference/get_option"]]get_option( 'meta_key', 'default value');[[/LINK]]

If you want to link it back to a collection of posts, then consider using an identifier in the post meta for each post that needs grouping (probably a number, but could be any unique key) and then store the value like this:

<?php update_option( '_meta_key_'.$grouping_key, $newvalue ); ?>

That way you can retrieve just the value you need and also join to it (with a CONCAT) in SQL if necessary.


Thomas Griffin comments:

Yes, I think I do need to save this as an option instead of post meta now that I begin to think about it. It needs to be accessible on a global scale instead of an individual scale. Let me look into this and see what I can come up with.


Thomas Griffin comments:

I've updated the questions so that hopefully it makes more sense. I don't know if a grouping key would work because the relationship is many-to-many. Am I making sense?


John Cotton comments:

<blockquote>I hope this clears things up.</blockquote>

:) Not really - it sounds crazy!

However, whilst you could get meta data to do what you want (just about) I think this is such a special requirement as to justify a separate database table storing the pairs of ids for posts that are connected. ie:


CREATE TABLE cpt_relationships (
object_id int(10) unsigned NOT NULL,
post_id int(10) unsigned NOT NULL
PRIMARY KEY (`object_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1; ';


I would then wrap the logic up inside a class (ie when you add a to b, the class adds b to a and when you delete a to be you delete b to a etc) so that you can always look up on the object_id column.

Arguably, you could make a class that did that for meta data, but you'd then be storing extra, unnecessary data in your database and that isn't good.

JC


John Cotton comments:

Sorry, that SQL should be:



CREATE TABLE cpt_relationships (
object_id int(10) unsigned NOT NULL,
post_id int(10) unsigned NOT NULL,
PRIMARY KEY (`object_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1; ';


...missed a comma out.

2011-11-15

web559 answers:

If I understand your question correctly, this looks like a situation for Posts 2 Posts, which is a very well-written many-to-many post relationship plugin by the WP core developer Scribu.

With P2P, you can set up reciprocal relationships by post type. Connect post types using some code in your functions.php file, and the plugin will generate meta boxes like the ones you describe, listing every post in the post type and allow you to "add" them to the current post.

Here's how it would work:

1. Install the plugin. Its Wordpress page is [[LINK href="http://wordpress.org/extend/plugins/posts-to-posts/"]]here[[/LINK]].

2. Add code to functions.php to register connections between your desired post types.
p2p_register_connection_type( array(
'from' => 'CPT_A',
'to' => 'CPT_B',
'reciprocal' => true,
'id' => 'cpt_a_to_cpt_b',
'title' => 'Your metabox title'
);

You would need 3 instances of the p2p_register_connection_type() function—one for A-B, one for B-C, and one for A-C. The 'id' and 'title' parameters are arbitrary and up to you.

3. On the page where you want to display the connected posts (assuming you do want to display them), add this to your template:

// Find connected pages
$connected = p2p_type( 'cpt_a_to_cpt_b' )->get_connected( get_queried_object_id() );

// Display connected pages
if ( $connected->have_posts() ) :
while ( $connected->have_posts() ) : $connected->the_post(); ?>

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endwhile;
wp_reset_postdata();
endif;

You can change the HTML display to suit your needs.

2011-11-14

Luis Abarca answers:

Well, you can store in a custom field or table the relations btween posts and post types.

So when you save post type "a", you get a custom field with the custom pos types to update and a list of ID's to update.

Can you explain a little more your post structure or a example of what you are trying to do ?


Thomas Griffin comments:

I've updated the question. Read it and see if that clears anything up.

2011-11-14

Julio Potier answers:

Hello

i don't think i get it:

3 post types : "CTP a" "CPT b" "CPT c".
1 meta field named "meta_field".
I'm on the post #123, a "CPT a" post type, and meta_field = "azerty".
Now i'm going to post #456, a "CPT b" post type, i update the meta_field with "qsdfqsdf".
Now, i'm back to post #123 and the meta_field value is "qsdfqsdf"

Am i right ?
if "yes", this is not a "meta_field" but a "site option" or "data transient", no ?

See you soon


Thomas Griffin comments:

I've updated the question so that it hopefully makes more sense.