1

Have a CPT with custom field "size" valued "big" or "small" and Author with custom radio field "gender". Author can be "Man" or "Woman". Need query posts if, for example, author is "man" and filed "size" is "big". Try something like this:

$args = array(
  'post_type' => 'custom_post_type',
  'meta_query' => array(
    array(
        'key'     => 'size',
        'value'   => 'big',
        'compare' => 'LIKE',
    )
  )
);
$query = new WP_Query( $args );

This show posts with key value "big" from all authors, but how add author with key "gender" valued "man" to this query?

2 Answers 2

0

By adding another array

   $args = array(
      'post_type' => 'custom_post_type',
      'meta_query' => array(
        array(
            'key'     => 'size',
            'value'   => 'big',
            'compare' => 'LIKE',
        ),
        array(
            'key'     => 'gender',
            'value'   => 'male',
            ),
      )

);
$query = new WP_Query( $args );
0

What you need is

  1. Create a list of users that match your search criteria
    $user_query = get_users( [
        'fields'        => 'ID',
        'meta_key'      => 'gender',
        'meta_value'    => 'male',
    ]);
    echo '<pre>'; print_r($user_query); echo '</pre>';
  1. Create a custom query of your custom post type where you also specify the author field:
    $my_custom_query = new WP_Query([
    'author__in' => $user_query,
    'post_type' => 'custom_post_type',
    'meta_query' => [
        [
            'key'     => 'size',
            'value'   => 'big',
            'compare' => 'LIKE',
        ]
      ],
    ]);

That should be it. ;-)

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.