Home Forums General Issues Find all articles that link to a specific book


Find all articles that link to a specific book

  • Hello!

    On my blog I have various post types, such as articles and books. With ACF I made a field ‘related_books’ to use with articles. In this field I can (optionally) link to multiple books.

    It is very easy to find all related books for an article.

    But what is the best way to do it the other way around?

    So when I am displaying a book, what is the most efficient way to find all articles that link to this book?

    Things I’ve tried:
    1) Querying for articles using wp_posts() and adding values for ‘meta_key’ and ‘meta_value’ –> I might have done it wrong but I got back all articles, even those that don’t mention any books at all.

    2) Taking it further one notch and using WP_Meta_Query. More work yet same effect as above

    3) Writing an SQL-statement that directly retrieves the right article-ID’s from the table ‘postmeta’. This works, but then I still need to fetch all article posts with these ID’s and it doesn’t really seem to be the most efficient and safe way to go

    4) Looping through all articles WP-style, and checking if the book on display is in their ACF field ‘related books’. Works. But also seems highly inefficient.

    What would be the best way to solve this? Best as in: efficient, safe and futureproof.

    This question doesn’t really seem unique, so if someone has already asked this (or solved this) I would be very happy with a reference too.

    Many thanks in advance 🙂

  • See this

    Or set up by directional field in code or with a plugin and then re-save all of the articles.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.