$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom field - ACF Post Object meta-query by title not ID|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom field - ACF Post Object meta-query by title not ID

matteradmin6PV0评论

I'm trying to query a post type (intervention) via a post object field (intervention_country) by the title (of the related custom post type - country).

Something like:

$wp_query = new WP_Query();
$wp_query->query(  array (
    'post_type' => 'intervention',
    'meta_query' => array(
        array(
            'key' => 'intervention_country',
            'value' => 'country_selected',
            'compare' => '=='
        )
     )
));

Can this be done? From other questions it looks like I have to give an ID but I would like to query using the title of the CPT (country) instead as given by `'country_selected' above

I'm trying to query a post type (intervention) via a post object field (intervention_country) by the title (of the related custom post type - country).

Something like:

$wp_query = new WP_Query();
$wp_query->query(  array (
    'post_type' => 'intervention',
    'meta_query' => array(
        array(
            'key' => 'intervention_country',
            'value' => 'country_selected',
            'compare' => '=='
        )
     )
));

Can this be done? From other questions it looks like I have to give an ID but I would like to query using the title of the CPT (country) instead as given by `'country_selected' above

Share Improve this question edited Nov 25, 2018 at 9:05 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 25, 2018 at 9:00 user142553user142553 212 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It can be done, but it won't be so easy. Post Object is stored as an ID, so you can't use meta query to search by post title in such case... There are few ways of solving that problem:

1. Use acf/save_post action to save title of given post object as another meta value:

// Add this to your functions.php
function save_intervention_country_name ( $post_id ) {
    $country_id = get_field( 'intervention_country', $post_id );
    if ( $country_id ) {
        update_post_meta( $post_id, 'intervention_country_name', get_post_field( 'post_title', $country_id ) );
    }
}
add_action( 'acf/save_post', 'save_intervention_country_name', 20 );

And then you can search using intervention_country_name field:

$wp_query->query(  array (
    'post_type' => 'intervention',
    'meta_query' => array(
        array(
            'key' => 'intervention_country_name',
            'value' => <COUNTRY NAME>,
            'compare' => '=='
        )
     )
));

2. Use posts_where and posts_join filters to modify SQL query.

You'll have to add a JOIN statement to join your post object field with posts table, and then add where searching by title.

3. Modify your form in such way, that it does the search by ID not by name.

So if there is a select field, then fill its values with IDs. Or if you can't do that, you always can get the country first and then use its ID:

$country = get_page_by_title( <COUNTRY_NAME>, OBJECT, <COUNTRY_POST_TYPE> );
if ( $country ) {
    $wp_query->query(  array (
        'post_type' => 'intervention',
        'meta_query' => array(
            array(
                'key' => 'intervention_country',
                'value' => $country->ID,
                'compare' => '=='
            )
         )
    ));
}
Post a comment

comment list (0)

  1. No comments so far