I have several custom type posts which I already filtered using WP Query.

From that list, I am trying to filter the posts that have a specific Custom Field value.

I tried using a meta_query, but the problem is that the select value does not exist yet. It only exists once the query has finished. I already have the select value stored in a variable.

<?php
$args = array(
    'post_type' => array(
        'one',
        'two',
        'three'
    ),
    'meta_query' => array(
        array(
            'key' => 'owner',
            //'value' => $currentSignedUser,
            //'value' => 'Owner'
        ),
    ),
);

$query = new WP_Query( $args );

echo '<h5>List of owned stuff: </h5><br />';

while($query->have_posts()) :
    $query->the_post();
    ?>
    <p><a href="https://wordpress.stackexchange.com/questions/245403/<?php the_permalink(); ?>"><?php the_title(); ?></a> <br />
    <?php
    $owner_select = get_field('owner');

    if ($owner_select) {
        echo 'Owner: ' . $owner_select[display_name];
    } else{
        echo '<p style="color:darkred"><strong>No associated owner for this item.</strong></p>' ;
    }
    ?> </p>
    <?php
endwhile;
wp_reset_query();

How can I filter the first query results with another query? Would this be the correct way or is it another method?

Thanks!

1 Answer
1

I solved my problem re-reading ACF’s custom fields documentation.

Instead of meta_query, I should’ve used meta_key.

The correct argument for a custom field is:

// args
$args = array(
    'numberposts'   => -1,
    'post_type'     => 'event',
    'meta_key'      => 'location',
    'meta_value'    => 'Melbourne'
);

Leave a Reply

Your email address will not be published. Required fields are marked *