0

I'm trying to figure out how to achieve a specific query to modify my search results for posts with WordPress. I'm trying to search via a custom field called "Common Authors".

There can be multiple Common authors, which is causing my query to sometimes fail. Here is what I've got for now:

<?php
...
$query->set('meta_key', 'common_authors');
$query->set('meta_value', serialize( array(strval($_GET['common_author'])))); // I get a single ID from the url as a string
$query->set('meta_compare', 'IN');

This is what I see when I var_dump the query:

'meta_key' => string 'common_authors'
'meta_value' => string 'a:1:{i:0;s:5:"17145";}'

This works fine if there is only one common_author.

However, there can be multiple common_authors for a post. Here is a meta_value example from the database:

a:4:{i:0;s:5:"14409";i:1;s:5:"17145";i:2;s:5:"14407";i:3;s:5:"14406";}

Could somebody help me out, to figure out how to adapt my query, so that it would return this one as well?

1 Answer 1

1

Try This One Work Prefect!

 $args = array(
        'post_type' => 'post',
        'meta_query' => array(
            array(
                'key' => 'common_authors',
                'value' => array ( 'author1', 'author2', 'author3' ),
                'compare' => 'IN'
            )
        )
    );
$query = new WP_QUERY($args);

if You Need To Use It In pre_get_posts

$meta_query = array(
                array(
                    'key' => 'common_authors',
                    'value' => array ( 'author1', 'author2', 'author3' ),
                    'compare' => 'IN'
                )
            );

$query->set('meta_query', $meta_query);
Sign up to request clarification or add additional context in comments.

2 Comments

That doesn't seem to work.. I'm overriding the default search via the pre_get_posts filter, I'm guessing that may have something to do with it.
Hi Code Updated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.