WordPress.org

Ready to get started?Download WordPress

Forums

Widget Logic
Conditional Tag to detect custom field w/in category (2 posts)

  1. schmedia@aol.com
    Member
    Posted 2 years ago #

    I need a conditional tag that will detect if there exists at least one post, in the same category as the current post, which has a specific custom field key/val pair.

    In other words, I am on a page associated with one and only one category I've defined (say, dog, cat, or mouse). For example, I'm on a 'dog' page.

    I'm trying to build a conditional tag that will display a given widget if, and only if, there exists a post, also in 'dog', with a custom field 'hair' with value 'long'.

    The same conditional tag would also return true for 'long' 'hair' cats and mice articles.

    I've wrestled with this all afternoon but my code is spectacularly failing to work. I think I'm badly mangling the get_posts() core function, but I just can't see it. Is this a solved problem for anyone here?

    Best regards,
    :Benjamin Barnett

    http://wordpress.org/extend/plugins/widget-logic/

  2. alanft
    Member
    Plugin Author

    Posted 2 years ago #

    that's quite a tall order! Once there is a post with "hair"="long" in category X it's easy to test if you are in category X

    in_category( array( 1,2,3 ) )

    so you could just update that array with the list of categories for which that is the case. it's manual work - and I suppose it depends how automatic you need it, and how often posts are added. Otherwise you are going to have to somehow cache the results of a query along these lines…

    for every post with metadata "hair"="long" give me the list of categories those posts are in

    and that list/array of categories fed into in_category does what you want

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic