Support

Account

Home Forums Front-end Issues Advanced Custom Fields – Meta Query no result

Solving

Advanced Custom Fields – Meta Query no result

  • Hey there,

    I am using WPML and ACF in my WP.

    Now I wanna list posts from the category ID 399 with the ACF Field “organization_type” and the value key “socialbusiness” but they do not show up.

    This are my query tries:

            $args = array(
                'post_type'     => 'post',
                'cat'      => 399,
                'posts_per_page'    => -1,
                'meta_query'        => array(
                    //'relation' => 'OR',
                    array(
                        'key' => 'organization_type',
                        'value' => 'socialbusiness',
                        //'compare' => '='
                    )
                )
            );
            //unset($args);
    
            $args = array(
                'numberposts' => -1,
                'post_type' => 'post',
                'cat' => 399,
                'meta_key' => 'organization_type',
                'meta_value' => 'socialbusiness'
            );
    
            // query
            query_posts( $args );
            while( have_posts() ) {

    What am I doing wrong?

  • Your code looks correct, however, query_posts is used to modify the main query of the post/page you’re on. You’ll probably want to use get_posts instead. The rest remains the same:

    
    $args = array(
      'post_type'     => 'post',
      'cat'      => 399,
      'posts_per_page'    => -1,
      'meta_query'        => array(
        array(
          'key' => 'organization_type',
          'value' => 'socialbusiness',
        )
      )
    );
    ?>
    
    <?php
    $social_businesses = get_posts( $args );
    
    if ( $social_businesses ): ?>
      <?php foreach ( $social_businesses as $sb ): ?>
        <a href="<?php echo get_permalink( $sb->ID ); ?>"><?php echo get_the_title( $sb->ID ); ?></a><br>    
      <?php endforeach; ?>
    <?php endif; ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Advanced Custom Fields – Meta Query no result’ is closed to new replies.