php - Wordpress get_posts() fetch posts with empty custom field -
i'm trying use get_posts()
function in wordpress retrieve list of posts if have empty custom field, called wpcf-translated-details
here current code:
<?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 ); ($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; } ?>
but couldn't find method or instructions on how achieve this.
so question is, how can modify code fetches 10 posts have empty wpcf-translated-details
custom field?
thanks
you can check meta_query
parameter wp_query
here.
$posts = new wp_query( array('post_type' => 'sale', 'numberposts' => 10, 'meta_query' => array( array( 'key' => 'wpcf-translated-details', 'value' => '', 'compare' => '=', ) ) ) );
Comments
Post a Comment