Passing a variable containing a comma separated list of values in a meta-query

In WordPress meta queries, when using the ‘compare’ => ‘IN’ comparison, the value should be an array, not a string. When you manually input array( ‘1302’, ‘1329’ ), it works because it’s correctly formatted as an array. However, when using $artists_string_value, it’s treated as a single string, not an array of values. You should modify … Read more

SQL command to delete bulk WP post specific

Deleting posts directly in the DB is not going to clear up all their comments, post metadata, etc. I’d recommend using wp_delete_post() to do this instead. // Gets the post IDs. $author_id = get_user_by( ‘login’, ‘newsagencies’ ); // Use the user’s login here. $posts = new WP_Query( array( ‘author’ => $author_id, ‘date_query’ => array( ‘after’ … Read more

WP_Query not using relation key as expected and not producing any results

From the developer docs: relation (string) – The logical relationship between each inner meta_query array when there is more than one. Possible values are ‘AND’, ‘OR’. Do not use with a single inner meta_query array. (Emphasis mine). It’s a bit hacky, but you could run a separate query against the meta field, then merge the … Read more

Unexpected number of loops in while

This is because have_posts() is for the main loop, and not your custom query. Prefixing each use of have_posts() and the_post() with $WatchListQuery-> will fix it: if ( $WatchListQuery->have_posts() ) { while ( $WatchListQuery->have_posts() : $WatchListQuery->the_post();

post loop with different design depending on post

Your $count++ needs to be after your endif statement. It’s not counting up because your counter is always at 0 which means you’re always in your “if” portion of your statement not your “else”. Like this: $args = array( ‘post_type’ => ‘post’, ‘post_per_page’ => 3, ); <?php $loop = new WP_Query($args); $count = 0; while … Read more

wp_logout action hook is not firing

In your code, get_current_user_id() will return 0 because this hook fires after the logout has happened, ie, there is no current user ID. If you want the $user_id to be available to your function, you have to pass it in. Note: the $user_id parameter was added in WordPress 5.5.0. If you’re using an older version, … Read more

How to use wp-query to search for posts where post_content OR post_title OR post_name

In WordPress 6.2+, within WP_Query you can set search_columns parameter (not documented at time of writing) to specify the fields to search: $query = new WP_Query( array( ‘s’ => ‘search term’, ‘search_columns’ => array( ‘post_content’, ‘post_name’, ‘post_title’ ), ) ); You can also use the post_search_columns filter to adjust the search columns. You may need … Read more