3

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="<?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!

7
  • Can you provide more context as your question is a little confusing, the title implies something involving pre_get_posts, but it sounds like you want to take the results of your query and find a subset of them at a later time, but if I take your question literally it doesn't say that – Tom J Nowell Nov 7 '16 at 21:34
  • I updated your code to be more readable by using coding standards and consistent indenting, but your loop should have an if ( $query->have_posts() ) check, should call wp_reset_postdata not wp_reset_query ( reset query is only to be used when cleaning up after a query_posts call ), and it should only call wp_reset_postdata if there are posts to cleanup ( aka if $query->the_post() was called ) – Tom J Nowell Nov 7 '16 at 21:40
  • I am not very well versed on Wordpress lingo, but I guess that's exactly what I want to do: filter a subset of my query results using a meta_query that filters with the value of a custom field selector. – MauF Nov 8 '16 at 2:24
  • BTW, I was following this tutorial: premium.wpmudev.org/blog/mastering-wp-query. Where can I read more about the changes you suggested, @TomJNowell? Thanks. – MauF Nov 8 '16 at 13:33
  • It's rare that people would take a WP_Query object, and then perform additional querying on stuff that's already been queried. Instead take the data/results it retrieved and create a second query. The other changes I suggested are all self explanatory, ask a question on the site if you need more detail – Tom J Nowell Nov 8 '16 at 15:38
2

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'
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.