Is it possible to Published all Draft Posts using SQL or Function code? I tried using this code in my function file, but not works.
add_action('admin_init','wpse_244394');
function wpse_244394(){
$args = array('post_type'=> 'post',
'post_status' => 'draft',
'posts_per_page'=>-1
);
$draft_posts = get_posts($args);
foreach($draft_posts as $post_to_publish){
$query = array(
'ID' => $post_to_publish->ID,
'post_status' => 'publish',
);
wp_update_post( $query, true );
}
}
try to use "wp_publish_post( $post_id )" instead of "wp_update_post".
add_action('admin_init','wpse_244394');
function wpse_244394(){
$args = array('post_type'=> 'post',
'post_status' => 'draft',
'posts_per_page'=>-1
);
$draft_posts = get_posts($args);
foreach($draft_posts as $post_to_publish){
wp_publish_post( $post_to_publish->ID );
}
}
Related
I have about 25,000 posts here (and rising). All of them under a Custom Post Type "lead".
Each post has meta information, including a variable called "uniqid".
this uniqid is a url parameter. now i need the post id where exactly this uniqid exists.
Now my question is if there is a way to speed up this determination.
I have tried 2 ways once a wp_query and get_results.
I ask because this function that determines the post id is always noted by plugin "query monitor" that it is slow.
These are the 2 approaches, both work. I just wonder if it is possible to speed up here?
The WP_Query solution.
$post_id = false;
$args = array(
'posts_per_page' => -1,
'post_type' => 'lead',
'fields' => 'ids',
'orderby' => 'date',
'order' => 'ASC',
'post_status' => 'publish',
'meta_key' => 'uniqid',
'meta_value' => $uniqid
);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
// Get Post ID for return
$post_id = get_the_id();
// Break loop, when matching id was found
$uniqidGPM = get_post_meta( $post_id, 'uniqid' , true );
if($uniqidGPM == $uniqid) {
$post_found = true;
break;
}
}
}
wp_reset_postdata();
return $post_id;
The select get_results solution.
$post_id = false;
global $wpdb;
$selectQuery = "SELECT wp_wkdm_posts.ID FROM wp_wkdm_posts INNER JOIN wp_wkdm_postmeta ON ( wp_wkdm_posts.ID = wp_wkdm_postmeta.post_id ) WHERE 1=1 AND ( ( wp_wkdm_postmeta.meta_key = 'uniqid' AND wp_wkdm_postmeta.meta_value = '".$uniqid."' )) AND wp_wkdm_posts.post_type = 'lead' AND ((wp_wkdm_posts.post_status = 'publish')) GROUP BY wp_wkdm_posts.ID ORDER BY wp_wkdm_posts.post_date ASC";
$selectQueryResult = $wpdb->get_results($selectQuery);
if (empty($selectQueryResult)) {
return $post_id;
}else{
$post_id = $selectQueryResult[0]->ID;
return $post_id;
};
Please use this meta_query condition on the same query, it helps you.
$args = array(
'post_type' => 'lead',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
'key' => 'uniqid',
'value' => 'YOUR_VALUE'
)
);
$query = new WP_Query($args);
When i try to add one post to db using wp_insert_post() in db added two posts:
Ajax request:
/wp-admin/admin-ajax.php?action=getchats&chat_type=all-chat&last_msg=110&add_msg=true&chat_message=helloworlds
action for this:
add_action( 'wp_ajax_getchats', 'getchats');
function getchats(){
if (!isset($_GET['last_msg'])||(!is_numeric($_GET['last_msg'])||(!isset($_GET['chat_type'])))){
die(json_encode(array('error' => 'no_latest')));
}
$cat_id = get_cat_ID($_GET['chat_type']); //the categories name
if ((isset($_GET['add_msg']))&&(isset($_GET['chat_message']))){
$user_id = get_current_user_id();
$description = $_GET['chat_message'];
$title = $description;
if (strlen($title)>20){
$title = mb_substr($title, 0, 20, 'UTF-8');
}
$my_post = array(
'post_content' => $description,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'chatmsg',
'post_author' => $user_id,
'post_category' => array($cat_id)
);
wp_insert_post($my_post);
}
$args=array(
'numberposts' => 3,
'orderby' => 'ID',
'category' => $cat_id,
'post_type' => 'chatmsg',
'post_status' => 'publish',
);
$messages = [];
$posts = get_posts($args);
die(json_encode($posts));
foreach( $posts as $post ){
if ($post->ID > $_GET['last_msg']){
$row = array(
'id' => $post->ID,
'message'=>$post->post_content,
'author'=>$post->post_author,
'date'=>$post->post_date,
);
$message[] = $row;
}
}
die(json_encode(array('error' => 'ok', 'messages'=> $messages)));
}
Why am i using only one wp_insert_post but receive two post?
UPD: Need use wp_doing_ajax for it. Thanks for answer Maxim Sarandi.
if( wp_doing_ajax() ) {
add_action('wp_ajax_getchats', 'getchats');
function getchats()
{
//some code
}
}
Maybe this or this might help you.
And can i give you a few recommendation to your code style?
First - use $_POST for similar queries.
Second - stop use die(wp_send_json()). Inside wp_send_json already present die(); Just use wp_send_json_error() for error response or wp_send_json_success(); or wp_send_json()
Third - use nonces and check_ajax_referer();
Fourth - stop cloning not needed brackets.
I need to print another shortcode when the posts count is 0
any code I add to this code return a fatal error
add_shortcode('reviews_total', 'reviews_total_func');
function reviews_total_func()
{
global $post;
$args = array(
'posts_per_page' => -1,
'post_type' => 'testimonial',
'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
'meta_value' => $post->ID,
);
$child_posts = get_posts($args);
return count($child_posts);
}
I need to view another shortcode if count($child_posts) = 0
Assuming the sole purpose of this shortcode is to output the number of posts, why not check for that prior to returning the value?
Example:
add_shortcode('reviews_total', 'reviews_total_func');
function reviews_total_func()
{
global $post;
$args = array(
'posts_per_page' => -1,
'post_type' => 'testimonial',
'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
'meta_value' => $post->ID,
);
$child_posts = get_posts($args);
if(count($child_posts) > 0)
return count($child_posts);
else
return "Your custom, alternate zero-posts text";
}
Execute another shortcode with the function do_shortcode
add_shortcode('reviews_total', 'reviews_total_func');
function reviews_total_func() {
global $post;
$args = array(
'posts_per_page' => -1,
'post_type' => 'testimonial',
'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
'meta_value' => $post->ID,
);
$child_posts = get_posts($args);
if(count($child_posts) > 0)
return count($child_posts);
return do_shortcode( '[my-short-code]' );
}
In my wordpress plugin i have follow code:
$args = array(
'post_status' => 'draft',
'category' => 1,
'post_type' => array('page', 'post', 'attachment' ),
'posts_per_page' => -1,
);
$allposts = get_posts($args);
foreach ( $allposts as $key => $post ) {
$update = [];
$update['ID'] = $post->ID;
$update['post_title'] = 'post title';
$update['post_name'] = 'post-name';
$post_id = wp_update_post( $update , true);
//fail, loop stopped without any errors
var_dump($post_id); //never happens
}
$allposts contains valid array of post objects
The first iteration update first post, but wp_update_post returns absolutely nothing, no errors, no post id, nothing
so next iteration never starts...
that code has works perfect before, but at one of the moment has broken
what happened here?
p.s. on other blog this code still works perfect
check this below code working in my system with publish post status.
add_action ('init',array( $this, 'updateAllPost'));
public function updateAllPost()
{
$args = array(
'post_status' => 'draft',
'category' => 1,
'post_type' => array('page', 'post', 'attachment' ),
'posts_per_page' => -1,
);
$allposts = get_posts($args);
foreach ( $allposts as $key => $post ) {
$update = array(
"ID"=>$post->ID,
"post_title"=>"post title",
"post_name"=>"post-name"
);
$post_id = wp_update_post( $update , true);
//fail, loop stopped without any errors
var_dump($post_id); //never happens
}
}
I want to change the defaults of the "wp_get_recent_posts" Function to get and order by title name.
I know how to do it with
$args=array('orderby'=> "title",'order'=> "ASC");
$recent_posts = $this->wp_get_recent_posts($args);
My question is how can i add these $args to the function wp_get_recent_posts from child-theme functions.php file with a hook/filter without editing other files.
Thanks.
Use function wp_get_recent_posts
<?php
$args = array(
'numberposts' => 10,
'orderby' => 'post_title',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish'
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
AFAIK there is no direct way to set a new "default" for the function.
The only feasable way I see is to write your own wrapper-function:
function wp_get_recent_posts_title( $args = array(), $output = ARRAY_A ) {
$defaults = array( 'orderby' => 'title' );
$args = wp_parse_args( $args, $defaults );
return wp_get_recent_posts( $args, $output );
}
You call this function just like the original, it will forward all parameters, with the exception that 'orderby' will be 'title' when it is not set explicitly.
Of course this will only work where you call it, it will not change any other calls to the original function.