Support

Account

Home Forums General Issues Taxonomy field by ids and wp_query

Solving

Taxonomy field by ids and wp_query

  • I am having a brain fart on an issue that I’ve done many times before…

    I’ve made an Ad system using a CPT instead of an Options page…
    Each ad post has ACF that controls it fully and I have a section that allows the user to select and assign the taxonomies to each ad so that I will know on which post taxonomies to place the ads… I need the taxonomies field to return “IDS” instead of OBJECTS…
    So my field returns as an array:

    Array ( [0] => 452 [1] => 506 [2] => 464 [3] => 472 [4] => 456 [5] => 744 [6] => 745 [7] => 428 [8] => 689 [9] => 681 [10] => 603 [11] => 606 [12] => 683 [13] => 639 [14] => 615 [15] => 601 [16] => 628 [17] => 621 [18] => 609 [19] => 552 [20] => 476 [21] => 740 [22] => 480 [23] => 447 [24] => 416 [25] => 453 [26] => 449 [27] => 550 ) 1

    I just need to assign a certain ID to the query below. The issue is that I am ONLY ABLE TO Assign 1 which works… But I need multiple. What am I doing wrong?

    Ive tried this which doesn’t work:

        'meta_query' => array(
            array(
                'key' => 'assign_to_taxonomies',
                'value' => array ( '689', '452', '744' ),
                'compare' => 'IN'
            )

    My working code is below:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
    $args = array(
      'post_type' => array('click_ads'), 
      'meta_query' => array(
          'relation' => 'AND',
    
        array(
              'key'     => 'select_ad_location', 
              'value'   => 'side', 
              'compare' => '=', 
              'type'    => 'CHAR', 
          ),
       
        
         array(
             'key'     => 'assign_to_taxonomies', 
             'value'   => serialize( strval('689') ),  //1 ID assign. but need multiple
             'compare' => 'LIKE', 
            'type'    => 'CHAR',
          ),
          
    
      ),
      'post_status' => 'publish', 
      'order' => 'DESC', 
      'orderby' => 'date', 
      'posts_per_page' => 1,
      'paged' => $paged, 
    );
    
    // WP_Query
    $eq_query = new WP_Query( $args );
    if ($eq_query->have_posts()) : // The Loop
    $eq_count = 0;
    ?>
    <div class="">
    
    <?php 
    while ($eq_query->have_posts()): $eq_query->the_post();
    $eq_count++;
    ?>
    
    <div class="ad-layout ad-cta">
        <div class="adtype"><?php the_field('enter_ad_code'); ?></div>
        <span class="adv align-center">ADVERTISMENT</span>
    </div>
    <?php endwhile; wp_reset_query(); ?> 
    </div>
    
    <?php endif;
  • To look for multiple values, this:

    
    'meta_query' => array(
            array(
                'key' => 'assign_to_taxonomies',
                'value' => array ( '689', '452', '744' ),
                'compare' => 'IN'
            )
    

    Would need to look like this:

    
    'meta_query' => array(
      'relation' => 'OR',
      array(
        'key' => 'assign_to_taxonomies',
        'value' => '"689"'
        'compare' => 'LIKE'
      ),
      array(
        'key' => 'assign_to_taxonomies',
        'value' => '"452"'
        'compare' => 'LIKE'
      ),
      array(
        'key' => 'assign_to_taxonomies',
        'value' => '"744"'
        'compare' => 'LIKE'
      )
    )
    
  • Thank Huebner!!!
    You always seem to come through!! A real ACF trooper!
    I think you meant to add the code to a code element… 🙂

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

You must be logged in to reply to this topic.