简体   繁体   中英

How do I modify Wordpress WP_Query with a custom field value after filtering?

I have three select boxes that allow a list of custom posts to be filtered, any or all of the options can be selected and then a "Filter" button is clicked which then reloads the page and appends the filtered variable(s) to the URL as a query string. This works perfectly fine with the two custom taxonomies I have:

$categories = array('company-sector', 'company-location');
foreach ($categories as $category) {
    if (isset($_GET[$category]) && !empty($_GET[$category])) {
        $query['tax_query'][] = array(
            'taxonomy'  => $category,
            'field'     => 'slug',
            'terms'     => $_GET[$category]
        );
    }
}

However, using the same logic on the third filter (a custom field created with ACF), the list of posts do not change:

if (isset($_GET['company-status']) && !empty($_GET['company-status'])) {
    $query['meta_query'][] = array(
        'key' => 'company_status',
        'value' => $_GET['company-status'],
    );
}

This meta query works just fine if I manually add it into the initial WP_Query like so:

$query = array(
    'post_type' => 'company-post',
    'posts_per_page' => $posts_per_page,
    'orderby' => 'title',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'company_status',
            'value' => 'acquired'
        )
    )
);

However, attempting to add it into the initial query after getting the variable from the query string doesn't seem to work, what am I doing wrong here?

//edit

Even this is working:

$query = array(
    'post_type' => 'company-post',
    'posts_per_page' => $posts_per_page,
    'orderby' => 'title',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'company_status',
            'value' => $_GET['company-status']
        )
    )
);

So it has to be something to do with how I'm appending the meta_query to the initial WP_Query...

I guess that you could use pre_get_posts for this in your functions.php

// Load our function when hook is set
add_action( 'pre_get_posts', 'js-modifying-main-query' );

function js-modifying-main-query( $query ) {
    $status = $_GET['company-status'];
    if($status && $status != ""){

        if( !is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'company-post' ) {

            $query->set('meta_key', 'company_status');
            $query->set('meta_value', $status);

        }

    }
}

Haven't tested it - just an initial thought.

//EDIT

To see your query variables, you can use this :

global $wp_query;
var_dump($wp_query->query_vars);

I'm still not sure why my initial code isn't working, but I've managed to come up with a workaround that seems to do what I need it to:

$status = false;
if (isset($_GET['company-status']) && !empty($_GET['company-status'])) {
    $status = array(
        'key' => 'company_status',
        'value' => $_GET['company-status'],
    );
}

$query = array(
    'post_type' => 'company-post',
    'posts_per_page' => $posts_per_page,
    'orderby' => 'title',
    'order' => 'ASC',
    'meta_query' => array($status)
);

//edit

Even this is working:

$status = false;
if (isset($_GET['company-status']) && !empty($_GET['company-status'])) {
    $status[] = array(
        'key' => 'company_status',
        'value' => $_GET['company-status'],
    );
}

$query = array(
    'post_type' => 'company-post',
    'posts_per_page' => $posts_per_page,
    'orderby' => 'title',
    'order' => 'ASC',
    'meta_query' => $status
);

But as soon as I try this, it fails:

if (isset($_GET['company-status']) && !empty($_GET['company-status'])) {
    $query['meta_query'][] = array(
        'key' => 'company_status',
        'value' => $_GET['company-status'],
    );
}

$query = array(
    'post_type' => 'company-post',
    'posts_per_page' => $posts_per_page,
    'orderby' => 'title',
    'order' => 'ASC',
);

You push the wrong value for company-status .

if (isset($_GET['company-status']) && !empty($_GET['company-status'])) {
    $query['meta_query'][] = array(
        array(
            'key' => 'company_status',
            'value' => $_GET['company-status'],
        )
    );
}

... makes this:

Array
(
    [0] => Array
        (
            [key] => company_sector
            [value] => val1
        )

    [1] => Array
        (
            [key] => company_status
            [value] => val2
        )

    [2] => Array
        (
            [0] => Array
                (
                    [key] => company_status
                    [value] => val3
                )

        )

)

You should push to meta_query array this way:

$query['meta_query'][] = array(
    'key' => 'company_status',
    'value' => $_GET['company-status'],
);

Also, if parameter relation is ommited, query would check for all the keys and values - but I assume you know this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM