Use Cron to modify posts via sql

Creating your own schedules is a little messy (because they need to be persistent and stored in database), but it is quite easy to hop on one of the native schedules that run twice a day. add_action( ‘wp_version_check’, ‘my_function’ ); function my_function() { //stuff } As for running actual queries you should use $wpdb for … Read more

cron job to auto delete posts of a specific post type older than x days

// add the schedule event if it has been removed if( ! wp_next_scheduled( ‘mg_remove_old_entries’ ) ) { wp_schedule_event( time(), ‘daily’, ‘mg_remove_old_entries’ ); //run the event daily } // action hooked to fired with wordpress cron job add_action( ‘mg_remove_old_entries’, ‘mg_remove_old_entries’ ); function mg_remove_old_entries() { $posts = get_posts( [ ‘numberposts’ => -1, ‘post_type’ => ‘vfb_entry’, ‘date_query’ => … Read more

WPDB: how to get the value of a field in a custom database table

OK, so first of all, you should always use proper escaping, when building the queries. This is how your code can look after fixing: global $wpdb; $value = $wpdb->get_var( $wpdb->prepare( ” SELECT column_name FROM {$wpdb->prefix}plugin_table WHERE ID = %d “, get_current_user_id() ) ); The things you have to take care of: You have to change … Read more

Block search SQL from happening

It’s possible to avoid SQL execution in WP_Query with the posts_pre_query filter. It’s e.g. used by plugins to outsource the default search to 3rd party search engines. Here’s an example how we can override the main search query on the front-end, with an empty posts array and avoid running the SQL search query: add_filter( ‘posts_pre_query’, … Read more

How do you use prepare when asking for a list of id’s

$wpdb->prepare() use the same syntax for formatting like php’s printf(). SSo you will need something like … WHERE post_id IN (%d,%d,%d) … This will fit for three ids. But what if we got more or less than three ids? We will use the php tools to create the needed formatting string (assuming $ids is an … Read more

Using $wpdb generates DB error

its a syntax error according to error message. starements like below might work for you. global $wpdb; $wpdb->show_errors(); $tableCustom = ‘join_users_defis’; $sql = “SELECT * FROM {$wpdb->postmeta}”; $requeteAffichage = $wpdb->get_row( $sql ); //or $requeteAffichage = $wpdb->get_results( $sql ); var_dump($requeteAffichage);

Backticks (`) Instead of Single Quotes (‘) in an SQL Statement?

Backticks and regular quotes (both single and double) have distinct meanings in SQL. Quotes indicate a literal string, whereas backticks are quoted identifiers. This isn’t specific to WordPress, but rather a general way in SQL of quoting columns or tables. For example, imagine you’re running a query comparing two columns: SELECT * FROM wp_posts WHERE … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)