# Quick Question about Programmatic Link to Existing Topic

**URL:** https://meta.discourse.org/t/quick-question-about-programmatic-link-to-existing-topic/186341
**Category:** WordPress
**Created:** [April 10, 2021, 7:05pm UTC](https://meta.discourse.org/t/quick-question-about-programmatic-link-to-existing-topic/186341 "2021-04-10T19:05:39Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![RichardC](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richardc/32/216860_2.png) [@RichardC](https://meta.discourse.org/u/RichardC)
#### Post date: [April 10, 2021, 7:05pm UTC](https://meta.discourse.org/t/quick-question-about-programmatic-link-to-existing-topic/186341/1 "2021-04-10T19:05:39Z")

</div>

I’m using frontend forms to create posts in WordPress that need to be linked to existing Discourse topics

My plan is to enter the existing topic URL into an ACF when I add or edit the WP post

Next I would use

`add_action('acf/save_post', 'my_acf_save_post', 25);`

Inside my\_acf\_save\_post function I would execute

`protected function link_to_discourse_topic( $post_id, $topic_url )`

Seen [here](https://github.com/discourse/wp-discourse/blob/master/admin/meta-box.php#L436)

Am I on the right track?

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [April 12, 2021, 1:15am UTC](https://meta.discourse.org/t/quick-question-about-programmatic-link-to-existing-topic/186341/2 "2021-04-12T01:15:02Z")

</div>

Yes, you’re on the right track. In broad strokes, what you need to do is

1. Retrieve the topic details from discourse (unless you have these already)
2. Save the relevant details to the right post meta fields

It’s those post meta fields that you see in that function you linked that create the connection.

I’ve also added this to my [WP Discourse](https://github.com/discourse/wp-discourse) dev agenda: to make it easier to do this, i.e. using a single public utility function to achieve this goal. I’ve slotted this in for June.

Also take a look at the existing utility functions in `lib/plugin_utilities.php` and `lib/utilities.php`. And the concomitant `link_topic` method in `admin/discourse-sidebar/discourse-sidebar.php`.

Let me know how you go.

---

<div class="post-metadata">

### Author: ![RichardC](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richardc/32/216860_2.png) [@RichardC](https://meta.discourse.org/u/RichardC)
#### Post date: [April 12, 2021, 2:08pm UTC](https://meta.discourse.org/t/quick-question-about-programmatic-link-to-existing-topic/186341/3 "2021-04-12T14:08:15Z")

</div>

@Angus, thanks for your response. I’m in PHP kindergarten, so if you’re planning to work on this, I’ll wait.

Also, I’m getting a repeatable error with the WP plugin. Don’t know if you want a separate post or GitHub issue, but here’s what’s happening:

When I open a WP post that is linked to a forum topic, the link does not show in the Discourse block. It looks like there is no link.

**Conditions:**  
This occurs whether the link was created by publishing a ‘New Topic’, or by ‘Link to Existing Topic’

My WP posts are CPTs.

My Commenting is set to “Display a link to the comments”

Other than that, there’s nothing special about my installation

**Confirmation:**  
I can confirm that links that **don’t** show in the Discourse block **do** exist by seeing them in my Admin columns, and because I can click on the frontend link and go to the forum post.

I’ll be happy to help with more information or testing.

Cheers, Richard

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [April 13, 2021, 3:29am UTC](https://meta.discourse.org/t/quick-question-about-programmatic-link-to-existing-topic/186341/4 "2021-04-13T03:29:52Z")

</div>

> [@RichardC](#):
>
> My WP posts are CPTs.

Have you added your custom post type in the “Post Types to Publish” pubishing setting? See further

> [@Install and configure the WP Discourse Wordpress plugin for Discourse](https://meta.discourse.org/t/wp-discourse-plugin-installation-and-setup/50752#heading--publishing-settings):
>
> To get started with the WP Discourse Plugin, go through each of these topics in order and watch the videos at the top of the topic. [Install the plugin](https://meta.discourse.org/t/install-the-wp-discourse-plugin/223489/1)[Connect to Discourse](https://meta.discourse.org/t/connect-wp-discourse-to-discourse/223490/1)[Set up Publishing](https://meta.discourse.org/t/wp-discourse-and-publishing-posts-to-discourse/223492/1)[Set up Commenting](https://meta.discourse.org/t/wp-discourse-and-commenting/223493/1)[Set up DiscourseConnect](https://meta.discourse.org/t/wp-discourse-and-discourseconnect/223494/1) The two main plugin features, Publishing and [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) don’t have to be used together. You can just set up Publishing (and Commenting), and not use [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true), or just set up [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true), or set up both. There’s a short description of each featu…

---

<div class="post-metadata">

### Author: ![RichardC](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richardc/32/216860_2.png) [@RichardC](https://meta.discourse.org/u/RichardC)
#### Post date: [April 13, 2021, 5:00pm UTC](https://meta.discourse.org/t/quick-question-about-programmatic-link-to-existing-topic/186341/5 "2021-04-13T17:00:54Z")

</div>

Yes. I should have mentioned, everything works at first: I can publish to a new topic, or link to an existing topic and the links work both ways …except

If I later re-edit a post on WP, the Discourse block looks like nothing is linked, even though my WP frontend link still goes to the correct forum topic, and the link in the forum topic still goes to the correct WP post.

So the problem only exists in the [WP Discourse](https://github.com/discourse/wp-discourse) block while re-editing a post.

Let me know if you need anything else.

---

<div class="post-metadata">

### Author: ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)
#### Post date: [April 13, 2021, 10:01pm UTC](https://meta.discourse.org/t/quick-question-about-programmatic-link-to-existing-topic/186341/6 "2021-04-13T22:01:33Z")

</div>

Thanks, a few more questions

1. Do you see any errors in your browser web inspector console?

2. If you do a full refresh on the page while viewing the editor, what happens?

It sounds like there’s a javascript issue. This could be caused by various things, including other plugins or a browser extension. We need to narrow it down a bit more if possible.

---

<div class="post-metadata">

### Author: ![RichardC](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richardc/32/216860_2.png) [@RichardC](https://meta.discourse.org/u/RichardC)
#### Post date: [April 14, 2021, 12:45pm UTC](https://meta.discourse.org/t/quick-question-about-programmatic-link-to-existing-topic/186341/7 "2021-04-14T12:45:30Z")

</div>

> [@angus](#):
>
> - Do you see any errors in your browser web inspector console?

Console: Node cannot be found in the current page

> [@angus](#):
>
> If you do a full refresh on the page while viewing the editor, what happens?

First the Discourse block shows Publishing Options / Pin Topic and Publish to Discourse sections. Then the Category section pops in. Does not show existing links.

> [@angus](#):
>
> other plugins or a browser extension

I turned off every plugin except WP-Discourse, CPT and ACF then tried it with a Brave browser and Edge browser, both with no extensions and shields off. Still can’t see existing links in the Discourse block.

I’m running a test between regular posts and CPT posts. I’ll let you know the results.

---

<div class="post-metadata">

### Author: ![RichardC](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richardc/32/216860_2.png) [@RichardC](https://meta.discourse.org/u/RichardC)
#### Post date: [April 15, 2021, 1:08pm UTC](https://meta.discourse.org/t/quick-question-about-programmatic-link-to-existing-topic/186341/8 "2021-04-15T13:08:21Z")

</div>

Not sure why, but my links are now showing in the Discourse block. I’ll report back if I find a pattern to this issue. Thanks.
