WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Im developing a smaller wp solution and we have both blog posts and products (custom post types) Id like to link them together so when you read blog posts it will suggest relevant products (that we have choosen) and when you look at products it will suggest blog posts (that we again have choosen).

Right now im achieving this by adding a customfield and populating it with, in the case of products, posts ids seperated by commas if more than one. E.g post_ID = 1,2,3,4,5

When i want to display them i just explode them and use get_posts with the argument post__in.

And it works fine! However the code i have created to handle the ux part behind the scenes is less than ideal, and i must admit i havnt been coding in a long time.

This is how i do it: Add a meta box with relevant stuff. Create a select box with all posts.

and use the following code to save the data:

I really do apologize if my code offends anyone, i tried my best :-)

function save_link_posts_to_ydelser(){
    global $post;
    // to combat the problem of the select is default on the empty option, thus deleting my custom field if admins saves!
    if($_POST["update"] == true) {

        // if admin wants to remove the linked products
        if ($_POST["artikler"] == '') {
            update_post_meta($post->ID, "post_ID", "");

        // we check if the field is set or not
        $meta = get_post_meta($post->ID, "post_ID", true);

        // its not
        if($meta == ''){
            update_post_meta($post->ID, "post_ID", $_POST["artikler"]);

        // it is
        } else {
            // we have to grab what ever is in there and combine it with the new data
            $existing_data = $meta;
            $new_data = $_POST["artikler"];
            $combined_data = $existing_data .",". $new_data;

            // we check if the chosen ID exists in the array already
            $existing_data_array = explode(",", $meta);
            if(in_array($_POST["artikler"], $existing_data_array)){
                // it does, we do nothing
            } else {
                // we update it with the new stuff
                update_post_meta($post->ID, "post_ID", $combined_data);


For some reason this changed my permalink aswell?

Thank you so much in advance!

share|improve this question
I would not proceed like that. I would use instead a special query to links post and custom post type according to tags. See wp_get_post_tags() to grab post tags. And then run a query to link posts with the same tags. – JMau Jun 8 '13 at 9:05
What is the save_link_posts_to_ydelser function hooked to? – s_ha_dum Jun 8 '13 at 12:48
add_action("save_post", "save_link_posts_to_ydelser"); – Anders Jun 8 '13 at 18:57
TweetpressFr, yea that would be one way of doing it, it just not that user friendly :-) – Anders Jun 8 '13 at 19:00
Had you looked into Posts 2 Posts plugin? It's staple for post relationships and probably would be more robust than solution made from scratch. – Rarst Jun 8 '13 at 20:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.