Как проверить, имеет ли пользовательское поле определенное значение - PullRequest
0 голосов
/ 06 июня 2019

Я пытаюсь создать новый пост (тип события) каждый раз, когда один из моих постов обновляется, и поле custom_field имеет значение 'ja'.Я не понимаю, почему не работает запрос if ($ customfield == 'ja') {}.

function create_event($post_id, $post, $post_before){
if (get_post_type($post) == 'product') {
    $customfield=get_post_meta($post_id, 'custom_field');
    if ($customfield == 'ja') { //this if query doesn't work
        $author = $post->post_author;
        $event = array(
            'post_type' => 'tribe_events',
            'post_title' => get_the_author_meta('firmenname', $author),
            'meta_input' => array('post_id' => $post_id, '_EventStartDate' => '2019-06-04 00:00', '_EventEndDate' => '2019-06-04 23:59',
                'test'=>$customfield)
        );
        wp_insert_post($event);
    }
}
add action('post_updated', 'create_event', 10, 3);

1 Ответ

0 голосов
/ 06 июня 2019
function create_event($post_id, $post, $post_before){
if (get_post_type($post) == 'product') {
    $customfield=get_post_meta($post_id, 'custom_field', true);
    if ($customfield == 'ja') { //this if query doesn't work
        $author = $post->post_author;
        $event = array(
            'post_type' => 'tribe_events',
            'post_title' => get_the_author_meta('firmenname', $author),
            'meta_input' => array('post_id' => $post_id, '_EventStartDate' => '2019-06-04 00:00', '_EventEndDate' => '2019-06-04 23:59',
                'test'=>$customfield)
        );
        wp_insert_post($event);
    }
}
add action('post_updated', 'create_event', 10, 3);

чтобы получить значение в переменной, вам нужно написать это $customfield=get_post_meta($post_id, 'custom_field', true);

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...