0

I have a query that finds the usage of an attachment in any post.

$used_in_content = array();

foreach ( $attachment_urls as $attachment_url ) {
    $content_query = new WP_Query( array(
        's'              => $attachment_url,
        'post_type'      => 'any',  
        'fields'         => 'ids',
        'no_found_rows'  => true,
        'posts_per_page' => -1,
    ) );

    $used_in_content = array_merge( $used_in_content, $content_query->posts );
}

However, the string can also be in the posts meta values. How can I change this query, so that it also looks in the post meta values? I tried out different things, but they didn't work. I'm really in the dark here, as I am not really familiar with how these queries work.

Edit: The string can only be found in one additional meta key.

  • 1
    It's not going to be possible with WP_Query without adding every possible meta key to a meta query. You'll probably need to use SQL. (I know this isn't an answer to the question, which is why I didn't post it as one). – Jacob Peattie Oct 3 '18 at 13:21
  • Ok, what if there is only one additional meta key that the string could be in? – TheKidsWantDjent Oct 3 '18 at 13:33
  • Simply, you cannot combine the search query with meta query. That's how WP_Query class is written. – ManzoorWani Oct 3 '18 at 13:52

Your Answer

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

Browse other questions tagged or ask your own question.