मुझे वर्डप्रेस wp_update_post () फ़ंक्शन के साथ एक त्रुटि मिल रही है जो अमान्य पोस्ट आईडी कहती है। यहाँ मेरा कोड है
$current_item = 273;
$my_post = array(
"ID" => $current_item,
"post_title" => "This is the post title.",
"post_content" => "This is the updated content.",
);
$post_id = wp_update_post( $my_post, true );
if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
foreach ($errors as $error) {
echo $error;
}
}
अग्रिम में धन्यवाद ।
उत्तर:
जवाब के लिए 2 № 1उपयोग "import_id"
, नहीं "ID"
.
यदि आपके द्वारा निर्दिष्ट आईडी पर कोई पोस्ट नहीं है, wp_update_post()
doesn "t एक नया बनाएँ - यह एक त्रुटि देता है। नए पोस्ट उपयोग की आईडी निर्दिष्ट करने के लिए "import_id" => $current_item
.
हालांकि, ध्यान दें कि यदि उस आईडी के साथ कोई पोस्ट है, import_id
एक अद्यतन के बजाय एक नई पोस्ट का कारण होगा। इसलिए यदि आप EITHER को उस ID के साथ एक नई पोस्ट बनाना चाहते हैं या उस ID पर पोस्ट अपडेट करना चाहते हैं, तो आपको इसकी आवश्यकता होगी if
अपनी चाबी लेने के लिए बयान:
$newPostKey = (get_post_status($current_item)) ? "ID" : "import_id";
// If there"s a post with an ID of $current_item, we"ll use "ID".
// Otherwise, use "import_id".
यहाँ आपका चमकदार नया कोड है।
$current_item = 273;
$newPostKey = (get_post_status($current_item)) ? "ID" : "import_id";
$my_post = array(
$newPostKey => $current_item,
"post_title" => "This is the post title.",
"post_content" => "This is the updated content.",
);
$post_id = wp_update_post( $my_post, true );