Home Forums General Issues get_terms, custom taxonomy & relationship


get_terms, custom taxonomy & relationship

  • Hello,

    I’m trying to create a simple taxonomy based ‘advert’ system. I’ve defined a custom taxonomy term offer_category with terms under it like Offer Type 1 and Offer Type 2. I want these offer types to be associated to specific pages on the site so I created a relationship field to select pages on one of these taxonomy terms e.g. Offer Type 1.

    I also have an Offers post type that has related offer categories as defined above which will be, by association to the offer category, linked to specific pages.

    What I need to do now is to pull out the offers that are related to a specific page and to do that I need to find the terms related to a specific page and then the offers (post type) related to those terms.

    In theory I think it sounds quite simple. However I can’t seem to pull out the terms using the following:

      $args = array(
        'taxonomy'        => 'offer_category',
        'meta_query'        => array(
            'key'           => 'offer_category_%_offer_pages', // custom field
            'compare'       => 'LIKE',
            'value'         => '"' . $page_id . '"'// match exactly "123"
      $terms = get_terms( $args );

    It looks like the offer_category_%_offer_pages field is set on the options table and I think that might be the problem but I’m not sure how to get round this. Can anyone point me in the right direction?

    Many thanks

  • Currently ACF does not save values in the term meta table, so they cannot be used in a term query. The developer is working on this and it will be included in a future release. There are several work-a-rounds posted in this topic for using term meta

  • Hello John,

    Thanks for the response, I think on balance it’s easier for me to query the options table directly:

    $terms = $wpdb->get_results( "SELECT CAST(TRIM(TRAILING '_offer_pages' FROM (TRIM(LEADING 'offer_category_' FROM option_name))) AS INT) AS term_id
      FROM ".$table_prefix."options WHERE option_name LIKE 'offer_category_%_offer_pages' AND option_value LIKE '%\"" . (int)$page_id . "\"%'");

    Interested to hear feedback on this but this will give me the term id’s I need which I can then hook back in to the ACF and WordPress functions to pull out the related offers.

    Many thanks

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

The topic ‘get_terms, custom taxonomy & relationship’ is closed to new replies.