0

I creating posts with wp_insert_post, and I want set for these posts canonical urls... How can I do this? Any idea is it would be fine.

In a perfect world I could use an option for this in my array... :

$new_post = array(
    'post_type' => 'my_post_type',
    'post_status' => 'publish',
    'post_author' => $user_id,
    'post_category' => array( 1,2,3 ),
    'canonical' => $canonicalurl // of course, this option dont exist
);
wp_insert_post($new_post);

(My first idea, that I set rel=canonical for my_post_type posts in a specific header-posttype.php, thus, that I add the $canonicalurl to the created post's content (because in my case the content of these post_type otherwise would be blank, so like:

'post_content' => $canonicalurl

And in the specific header I use the $content in the rel, like:

 global $post;
    $content = $post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);

 echo '<link rel="canonical" href="' . $content . '" />';

Or I also can do that with a custom taxonomy (or queryable custom metadata), what would be solution suchlike (more relevant for the most developer) cases, when the post really have content, but I dont know, that these rel=canonical datas will be callable with the wp_get_canonical_url() function (and this is first priority for me...) ... I will test this now, but I would be glad for a safer/more elegant solution. ))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.