Reputation: 239
I would like to associate custom post types posts which are products with the standard wordpress posts. Can anyone point me in the right direction how to accomplish this?
The reason being is i want to write blog articles and some of my blog articles may relate to a specific product. So on the products page i want to be able to have a link of related articles to that product.
Upvotes: 0
Views: 396
Reputation: 19366
I would suggest to save an array or string of the IDs of related posts / related product as using a custom field in the backend of your Website.
On the frontend of your Website you can get the associated IDs from the Database and Make Links using the functions <?php echo get_the_title(ID); ?>
and <?php echo get_page_link(ID); ?>
.
Depending on how comfortable the backend must be to edit, you can add custom Meta Boxes to the backend, using the following code in functions.php.
add_action('admin_init', 'register_meta');
add_action('save_post', 'save_metadaten');
function register_meta(){
add_meta_box("produkt_meta","Daten des Produkts","produkt_meta","produkt","normal","high");
}
function produkt_meta() {
global $post;
$custom = get_post_custom($post->ID);
$preis = $custom["produkt_preis"][0]; ?>
<h4>Produkt-Daten</h4>
<p style="padding-bottom:4px;"><label style="width:200px; display:inline-block;">Preis:</label><input size="5" name="produkt_preis" value="<?php echo $preis; ?>" /> €</p>
<?php
}
function save_metadaten(){
global $post;
// check if there are associated post IDs set somehow, and prepare these to save them in the database
if($_POST["produkt_preis"]) {update_post_meta($post->ID, "produkt_preis", $_POST["produkt_preis"]);}
}
Replace "produkt_preis" with "associated_post_ids" or whatever.
For maximum comfort you can add a JavaScript to the produkt_meta function which lists your posts so you just click it and the meta-field is automatically filled, but this is the part you should create on your own now ;-)
Hope this helps.
Upvotes: 1