Adding Variables to post query

As toscho said, you can modify the query in the pre_get_posts hook. That hook gets the query object passed as an argument, so you don’t have to read a global variable. add_action( ‘pre_get_posts’, ‘wpse12692_pre_get_posts’ ); function wpse12692_pre_get_posts( &$wp_query ) { if( isset( $_SESSION[‘size’] ) && $_SESSION[‘size’] != ‘all’ ) { $wp_query->query_vars[‘meta_query’] = array( ‘key’ => … Read more

Nginx rewrite rules

The correct Nginx rewrite rules for WordPress are: location / { try_files $uri $uri/ /index.php?q=$uri&$args; } This sends everything through index.php and keeps the appended query string intact. If your running PHP-FPM you should also add this before your fastcgi_params as a security measure: location ~ \.php { try_files $uri =404; // fastcgi_param …. // … Read more

Query from a different database than the default

I don’t know why the mysql_connect isn’t working inside WordPress. But an easier way of doing it would be to use the $wpdb class. Try replacing your code with this: $second_db = new $wpdb( ‘user’, ‘pass’, ‘dbname’, ‘host’ ); $q = $second_db->get_results( ‘SELECT * FROM vars’ ); var_dump( $q ); and see if you get … Read more

WordPress Find Duplicate Post By Content

Try to use the following SQL query to fetch duplicate posts: SELECT p2.* FROM wp_posts AS p1 LEFT JOIN wp_posts AS p2 ON p1.post_content = p2.post_content AND p2.ID < p1.ID AND p1.post_type = p2.post_type AND p1.post_status = p2.post_status WHERE p1.post_type=”post” AND p2.ID IS NOT NULL

How to echo excerpts with wp_list_pages?

If you want to make use of all the nifty filters for the title and excerpt/content (and why would you not want that?) you should loop through a custom query instead of using get_pages and the pages’ plain contents: <?php $args = array( ‘post_type’ => ‘page’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => -1, ‘post_parent’ => $post->ID, … Read more

How can I apply a meta query for a single custom post type in the main query?

In the case that the start_date custom field only exists for the event post type, this works: $query->set( ‘meta_query’, array( ‘relation’ => ‘OR’, array( ‘key’ => ‘start_date’, ‘compare’ => ‘NOT EXISTS’, ), array( ‘key’ => ‘start_date’, ‘value’ => $today = date( ‘Ymd’ ), ‘compare’ => ‘>=’, ), array( ‘key’ => ‘end_date’, ‘value’ => $today = … Read more

wpdb::prepare() isn’t working

The issue is that in My/SQL table names are not strings. So ‘wp_product_codes’ is being interpreted as a string instead of as an actual table. This is why you usually don’t see tables passed into wpdb::prepare() – you should know the table names as they won’t change where wpdb::prepare() is meant for data that you … Read more

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