Why my query does not run with prepare

That is not how $wpdb->prepare works. You feed prepare a string with sprintf-like placeholders, and the appropriate replacement values. Placeholders The query parameter for prepare accepts sprintf()-like placeholders. The %s (string), %d (integer) and %f (float) formats are supported. (The %s and %d placeholders have been available since the function was added to core in … Read more

Custom search SQL Query to add custom field in result

I got it ! Here is my final code for people who will search for similar problem 🙂 in functions.php : function se190254_custom_sql_for_search( ) { global $wpdb; //For security escape the search parameter $search_string = esc_sql($_GET[‘s’]); //Build the SQL request with $wpdb->base_prefix for “portability” $sql = “SELECT SQL_CALC_FOUND_ROWS {$wpdb->base_prefix}posts.ID FROM {$wpdb->base_prefix}posts LEFT JOIN {$wpdb->base_prefix}icl_translations t … Read more

Updating with $qpdb->query() always returns 0 rows affected

wpdb uses PHP’s mysqli_affected_rows() or mysql_affected_rows(), which can be disabled via an argument in php.ini. I am guessing that that is what has happened. Check the disabled_functions line in your php.ini file and remove the references to those function. Of the top of my head, I am not sure if extensions like Suhosin mess with … Read more

How do I get the index for a custom post?

For example if there are 10 custom posts, and No. 1 is the oldest, and they happened to select the 3rd oldest one, they want it to say “No. 3”. There is a more elegant way than that function: $args = array( ‘post_type’ => ‘book’, ‘fields’ => ‘ids’, ‘ignore_sticky_posts’ => true, ‘orderby’ => ‘post_date’, ‘order’ … Read more

Mysql query not working on WordPress 4.2.2

Try “bootstrapping” WordPress for your custom script first: include ‘../../../wp-load.php’ (you might need to adjust the path for your needs). Then you can safely use the $wpdb class, such as $wpdb->get_results and other goodies. Don’t forget to escape/clean your incoming data, if any. WordPress documentation on $wpdb PS: always try to use “builtin” functions. The … Read more

Hide text if query has no posts

Remove line#5 and do the following: if ($query->have_posts()) : echo ‘<h2 class=”seccion col-sm-12″>Tour</h2>’; //we are showing the div inside the conditional I removed unnecessary <div>, as <h2> itself is a block element and can do the trick for you.