Why query_vars get altered in WP_Query Object?

Milo’s comment about pre_get_posts helped solve this issue for me. In my case the parent theme used the pre_get_posts action to explicitly set the post_type for all author pages. It wasn’t in the functions.php, but I was able to find it by searching the theme for instances of pre_get_posts. (I used grep -R “pre_get_posts” . … Read more

Rewrite URL Parameter And Force ‘Pretty’ Permalink

You could force the redirect this way by checking for the get_var query value: add_action(‘init’,’my_redirect_check’); function my_redirect_check() { if (isset($_GET[‘my_var’])) { if ($_GET[‘my_var’] != ”) { if ($_SERVER[“HTTPS”]) {$location = ‘https://’;} else {$location = ‘http://’;} $location .= $_SERVER[‘SERVER_NAME’]; $location .= strtok($_SERVER[‘REQUEST_URI’],’?’); $location = trailingslashit($location); $location .= $_GET[‘my_var’]; $location = trailingslashit($location); wp_redirect($location); exit; } } } … Read more

Passing array of strings to a SQL statement in a WordPress plugin

$wpdb->prepare() accepts a single array of arguments as the second parameter, e.g. $wpdb->prepare( “SELECT * FROM table WHERE field1 = %s AND field2 = %d AND field3 = %s”, array( ‘foo-bar’, 123, ‘[email protected]’ ) ) Therefore you can use an array of placeholders with the SQL command (the 1st parameter), like so: // Create an … Read more

Rewrite rules and query for virtual page

You can create a posts archive by just setting post_type=post in your rewrite rule: function custom_archive_rule() { add_rewrite_rule( ‘custom-archive/?’, ‘index.php?post_type=post’, ‘top’ ); } add_action( ‘init’, ‘custom_archive_rule’ ); WordPress will identify this as is_home, so you’ll have to target it in pre_get_posts by the existence of your extra query variables. function custom_arcive_tax( $query ) { if … Read more

WordPress rewrite rules for state and city

Wasn’t as hard as I thought. Here’s the working example, in case anyone finds it helpful: function em_query_vars($vars) { array_push($vars, ‘state’); array_push($vars, ‘city’); return $vars; } add_filter(‘query_vars’,’em_query_vars’); function em_rewrite() { add_rewrite_rule( ‘^state/([^/]*)/([^/]*)/?’, ‘index.php?state=$matches[1]&city=$matches[2]’, ‘top’ ); add_rewrite_rule( ‘^state/([^/]*)/?’, ‘index.php?state=$matches[1]’, ‘top’ ); add_rewrite_tag(‘%state%’,'([^&]+)’); add_rewrite_tag(‘%city%’,'([^&]+)’); } add_action(‘init’, ’em_rewrite’); function em_templates($template) { global $wp_query; if (isset($wp_query->query_vars[‘state’]) && isset($wp_query->query_vars[‘city’])) { … Read more

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