Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress get_posts()获取自定义字段为空的帖子_Php_Wordpress - Fatal编程技术网

Php Wordpress get_posts()获取自定义字段为空的帖子

Php Wordpress get_posts()获取自定义字段为空的帖子,php,wordpress,Php,Wordpress,我试图使用Wordpress中的get_posts()函数来检索帖子列表,但前提是它们有一个空的自定义字段,该字段称为wpcf translated details 这是我目前的代码: <?php require_once('wp-load.php'); $temp_list_of_products_array = get_posts( array('post_type' => 'sale', 'numberposts' => 10 ) )

我试图使用Wordpress中的
get_posts()
函数来检索帖子列表,但前提是它们有一个空的自定义字段,该字段称为
wpcf translated details

这是我目前的代码:

    <?php
    require_once('wp-load.php');
    $temp_list_of_products_array        = get_posts( array('post_type' => 'sale', 'numberposts' => 10 ) );
    $temp_list_of_products_array_length = count( $temp_list_of_products_array );

    for ($xt = 0; $xt < $temp_list_of_products_array_length; $xt++) {
        $temp_product_id                    = $temp_list_of_products_array[$xt]->ID;
        $temp_product_untranslated_field    = get_post_meta($temp_product_id, 'wpcf-product-details', true);
        $temp_product_translated_field      = get_post_meta($temp_product_id, 'wpcf-translated-product-details', true);
        $temp_product_description_language  = 'en';

        if ($temp_product_translated_field == null) {
            $temp_product_translated_contents   = google_translate_text($temp_product_untranslated_field, $temp_product_description_language);
            update_post_meta($temp_product_id, 'wpcf-translated-product-details', $temp_product_translated_contents);
        } 
        echo $temp_product_id;
    }
    ?>

但我找不到任何方法或指示如何实现这一点

所以我的问题是,如何修改这段代码,使它只获取10篇文章,其中包含一个空的
wpcf translated details
自定义字段


谢谢

您可以检查
WP\u查询的
meta\u查询
参数

$posts = new WP_Query(
    array('post_type' => 'sale',
        'numberposts' => 10,
        'meta_query' => array(
            array(
                'key' => 'wpcf-translated-details',
                'value' => '',
                'compare' => '=',
            )
        )
    )
);

您好,谢谢您的回复,在get_posts()函数中不能使用“meta_query”吗?我一直得到“致命错误:调用未定义的函数WP_query()”,尽管我已经包含了“include('WP-load.php');”在我的php文件中,
WP\u Query
是一个类……您应该有
$posts=new WP\u Query()
。上面的答案有一个输入错误。
meta\u查询
no。但是
meta\u键
meta\u值
参数是。查看示例:
var\u dump($temp\u list\u of\u products\u array)
查看您的结构。