Answer the question
In order to leave comments, you need to log in
How to go through all the posts in the plugin?
I'm making a plugin that will check if the post has an image or not, I need to pass $post_id to the function, but I don't know where to get it
/*
Plugin Name: WPM Image Holder
Plugin URI: https://wp-masters.com
Description: Set Thumbnail to All Posts where Image is not set
Author: WP Masters
Version: 1.0
*/
class ImageHolders {
/**
* Initialise functions
*/
public function __construct()
{
// Put hooks here and look at the example how set function to hooks
add_action('init', [$this, 'check_post_is_has_thumbnail']);
}
/**
* Check Posts is has Image before Loop in FrontEnd
*/
public function check_post_is_has_thumbnail($post_id)
{
// Your function is going here
}
}
new ImageHolders();
Answer the question
In order to leave comments, you need to log in
Any request get_posts()
or wp_query()
, in a loop, check what you need.
But this is a rather expensive action, you should not perform it every time init is loaded. You should run it either on a button click or a cron, or replace it with checking for the presence of only the current image
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question