0

I'm trying to create a glossary for my WordPress site, using a custom post type 'glossary'.

My idea is to create posts for each term/word I want to explain (for example: 'blepharoplasty'), and use the content of the post to provide a description.

This works fine standalone with single post pages, and an archive to display all of them.

But I want to hook this into the rest of my site and automatically find any instances of my custom post titles in post meta data and the_content, and if found, to provide a link to the single post page.

I know a filter/hook is needed, but I have no idea how to write the conditions.

I think 121679 is somewhere along the lines, but I just don't know how to reference my custom post type, and how to search against the current post I'm using.

Can anyone help?

function bv_glossary_content_links($null, $object_id, $meta_key, $single ) {
// What conditions can go here?
}
add_filter('get_post_metadata', 'bv_glossary_content_links', 10, 4);
PPL
  • 6,357
  • 1
  • 11
  • 30
Lee
  • 4,187
  • 6
  • 25
  • 71
  • I don’t think `get_post_metadata` is the right thing to hook into to begin with. You should do this when you _output_ this meta data in certain contexts, but not generally. (Imagine f.e. a meta data field that gets output in the page’s meta description - you would not want “links” in there.) And `// What conditions can go here?` is rather broad ... at that point you obviously would have to first of all get a list of all your “glossary” posts, so that you can then loop over those to see which titles are contained in your current post’s meta data/main content ... – CBroe Apr 03 '18 at 10:17
  • Well, yes I didn't mention that there's only a few post_meta keys that I want this to affect. I was't sure if I'd need a different filter for each one perhaps, or just have one that has a large condition inside. Whichever is more efficient. – Lee Apr 03 '18 at 10:20
  • My broadness is purely because I'm not sure if I can do a simple get_posts here, because I'd only want to filter out posts where my matched word is in the title. – Lee Apr 03 '18 at 10:21
  • What “filtering” are you talking about now? So far you said you wanted to replace content, what’s that got to do with filtering now? – CBroe Apr 03 '18 at 10:22
  • I could be using the wrong terms, but surely there has to be a filter somewhere in there so it only retrieves the posts or post that matches the word found in the content? I'm guessing this must be quite complex, but in theory, posibly not as there are lots of SEO plugins that seem to be able to do these hooks into the_content as others. – Lee Apr 03 '18 at 10:35
  • I don’t think it makes any sense, performance-wise, to try and select only such specific glossary posts to begin with - especially when you want to apply this to more than one (non-glossary) post at a time. I would get a list of all glossary posts (and cache that somehow, so as to not request that same data again every time a filter runs), and then for the individual post content that you want to handle loop over those and see which ones are contained in the post content and need replacing. – CBroe Apr 03 '18 at 10:41
  • The right order to do this in might present an additional complication; you would not want to go and replace such glossary words in what might be HTML code at that point already. – CBroe Apr 03 '18 at 10:41

0 Answers0