How to reset usual $query on search page to push custom $wpdb query there?

This is not how pre_get_posts works. The pre_get_posts action gives developers access to the $query object by reference (any changes you make to $query are made directly to the original object – no return value is necessary). https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts What you are doing is simply wrong. You don’t “return” another query via pre_get_posts, you alter the … Read more

Problem in exploding the_content to array

You have a couple of issues here the_content() echos the content to screen. You should be using get_the_content() which returns the content. Just remember, get_the_content() is unfiltered, so if you need filtered content, use apply_filters( ‘the_content’, get_the_content() ) which will return filtered content. Your explode() function is probably wrong as well. You are using a … Read more

empty query breaks other queries

Before we start, you should properly indent your code and remove php spam. This makes your code much easier to read and understand. Secondly, rather use curlies ({}) instead of syntax like :, endif and endwhile. It is easier to debug and to read. Just another tip, comment your code for future reference, it makes … Read more

Custom Query – Based on user input

While this is primarily a SQL question, there are WordPress components and Core has no efficient mechanism to pull this data that I am aware of. This has long been a problem in Core in my opinion. So: global $wpdb; // group name key $meta_key = ‘_create_new_group’; if (!empty($_REQUEST[‘q’])) { // user input $search = … Read more

Post with certain term and without any term

This should be possible with the tax_query parameters for WP_Query. The query is either for posts where any term NOT EXISTS, OR, for any posts where the term IDs are IN the provided array. ‘tax_query’ => array( ‘relation’ => ‘OR’, array( ‘taxonomy’ => ‘group’, ‘operator’ => ‘NOT EXISTS’, ), array( ‘taxonomy’ => ‘group’, ‘field’ => … Read more

sort query results by newest

WP_Query(array(‘post_type’=>’portfolio_item’, ‘post_status’=>’publish’, ‘posts_per_page’=>-5, ‘order’ => ‘ASC’)); See: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters for order parameters. Hope that helps.

get csv of users with user_meta

SELECT u.ID, u.user_email, u.display_name, um1.meta_value AS first_name, um2.meta_value AS last_name, um3.meta_value AS phone_number, um4.meta_value AS salutation, um5.meta_value AS title FROM wp_users u LEFT JOIN wp_usermeta um1 ON u.ID = um1.user_id AND um1.meta_key = ‘first_name’ LEFT JOIN wp_usermeta um2 ON u.ID = um2.user_id AND um2.meta_key = ‘last_name’ LEFT JOIN wp_usermeta um3 ON u.ID = um3.user_id AND … Read more

wpdb COALESCE won’t work

From what I could see, the query did return a result, but the COALESCE() result was not named, hence MySQL and WordPress use the entire COALESCE() query as the field name. To avoid that, you would use an alias for the COALESCE(), e.g. //$query = “SELECT COALESCE( <your queries> ) AS <alias>”; $query = “SELECT … Read more

Why my query ‘REPLACE INTO…’ does not work?

The reason is due to this code inside dbDelta() : // Create a tablename index for an array ($cqueries) of queries. foreach ( $queries as $qry ) { if ( preg_match( ‘|CREATE TABLE ([^ ]*)|’, $qry, $matches ) ) { $cqueries[ trim( $matches[1], ‘`’ ) ] = $qry; $for_update[ $matches[1] ] = ‘Created table ‘ … Read more

How to get events using multiple custom meta fields?

You can use your current code to create the query and then in your loop run a check before displaying the events something like <?php query_posts(‘meta_key’ => ‘end_date_value’, ‘meta_compare’ => ‘>’, ‘meta_value’ => $todaysDate); while ( have_posts() ) : the_post(); $post_custom = get_post_custom($post_id); if ($startDate < $post_custom[‘start_date_value’]){ //your loop here } } ?> keep in … Read more