MySQL variable in query

See the documentation for $wpdb->prepare(). You need to pass a String, but you’re typing set @csum directly in the parameter. Your code should look like this: if ( is_user_logged_in() ) { $user = wp_get_current_user(); $balance = $wpdb->query( $wpdb->prepare( “SET @csum := (SELECT current_balance FROM exp_ten WHERE tenant_number=%d); SELECT tenant_number, transaction_amount, (@csum := @csum + transaction_amount) … Read more

How to export current year posts from WordPress?

The core version of WordPress (i.e. with no plugins installed) has export functionality built-in. You will find the export feature in the main WordPress admin menu in the Tools section. There are three options available. The first option backs up all content including posts, pages, comments, custom fields, categories and tags. Alternatively, you can choose … Read more

add_query_arg() and empty variables inside

You can instantiate your array before-hand, optionally populate it, and pass it to the add_query_arg() function like so: $url_query_args = array(); if( isset( $search ) ) { $url_query_args[‘search’] = $search; } if( isset( $category ) ) { $url_query_args[‘category’] = $category; } if( isset( $filter ) ) { $url_query_args[‘filter’] = $filter; } esc_url( add_query_arg( $url_query_args, ‘/site’ … Read more

How to display related posts by same publish date?

Get the date of current post: $date = get_the_date(‘Y-m-d’); Get year, month and day from the $date variable. $exploded = explode(‘-‘, $date); $year = $exploded[0]; $month = $exploded[1]; $day = $exploded[2]; Lastly, query the posts that has the same publish date. $args = array( ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘date_query’ => array( array( ‘year’ … Read more

Query Posts By Post Publish Date, but sort by Custom Meta Key

As you’ve discovered, meta queries only work with the post meta table, it is indeed looking for a meta key of post_date. You can’t do timespan queries with a simple query, but we can accomplish this with a combination of a query for the meta key, and a posts_where filter directly on the SQL to … Read more

Order query by post meta value

Take a look at the WordPress Codex: http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_orderby function edit_posts_orderby($orderby_statement) { $orderby_statement = “meta_value DESC”; return $orderby_statement; } add_filter(‘posts_orderby’, ‘edit_posts_orderby’); If you’re making the WP_Query yourself you can change the orderby parameter to meta_value Take a look here: http://codex.wordpress.org/Class_Reference/WP_Query

WordPress Loop: List All Posts by a Category & Subcategory

The cat and category_name parameters for WP_Query display “posts that have this category (and any children of that category)”. There are also examples of doing this in the Codex. $query = new WP_Query( ‘cat=4’ ); // or $query = new WP_Query( ‘category_name=staff’ ); I am not 100% sure what you mean by “I need all … Read more

Make one query for adding entries to database

Is there a $wpdb method available that will allow me to create the query, by looping through my query, and then execute it once I’m out of that loop? If you and your loop can construct the proper SQL, then use $wpdb->query. You can run any query you want with that, so whatever version of … Read more

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