Make a SQL query with wpdb in WordPress

The main problem is how you’re specifying the custom table name (e.g., $wpdb->cf7dbplugin_submits). $wpdb only knows about the “built-in” tables when accessing a table name via $wpdb->xxx. To specify access a custom table name, use {$wpdb->prefix}custom_table_name. The other thing I notice is that, for security purposes, you should never use interpolated variables in an SQL … Read more

WP User Query with Custom Fields and Search Results

Not sure if you ever found the answer, but just in case, I just did 🙂 There’s a hook I (finally!) found that lets you do this and works in a similar way to posts_where. The hook is pre_user_query. Here’s an example on the Codex: https://developer.wordpress.org/reference/hooks/pre_user_query/#user-contributed-notes With the above, your function could now look like … Read more

WP_Query gives me different results depending on the category order

I solved it the following way $query_args[‘tax_query’] = array( ‘relation’ => ‘OR’, array( ‘taxonomy’ => ‘category’, ‘field’ => ‘id’, ‘terms’ => $categories, ), ); Apparently I had to explicitely say that I was filtering through the taxonomy “category”. I could see that thanks to the comment from @birgire. Thanks man! Update As petition, this is … Read more

Replacing search results with custom external query

You can’t return a query object from a pre_get_posts action. In fact, it shouldn’t return anything, the query object is passed by reference. Your second attempt is close, you can unset the s query var so WordPress doesn’t try to search on that term. Of course, now s has no value, so any functions that … Read more

pre_get_posts – Trying to get property of non-object warning

$screen may not be available yet when pre_get_posts fires. Try this instead: public function my_post_type_default_sort( $query ){ if(!isset($_GET[‘orderby’]) && is_admin()) { $orderby = ‘0’; } if($query->is_main_query() && is_admin() && is_post_type_archive(‘my_post_type’) && $orderby=0) { $query->set(‘orderby’,’scr_date’); $query->set(‘order’, ‘ASC’); } }

how would i change post->ID to work correctly when querying pages?

Try with get_the_ID() $image_uploaded_meta_id = get_post_meta(get_the_ID(), ‘_listing_image_id’, true); get_the_ID() is a core WordPress function that acts directly inside the WordPress Loop, retrieving the id of the current item being loaded. $post->id is not working because the variable $post has no scope inside the loop. Here’s the link to the documentation: https://developer.wordpress.org/reference/functions/get_the_id/