Count number of published posts by type

If you prefer the $wpdb direct queries you can use something like this: global $wpdb; $count = $wpdb->get_var( “SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_type=”code” and post_status=”publish”” ); echo $count; In the sql query above change the post_type to whatever you like. This will return count of published posts under specific post type only. If you … 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

Query_posts $query_string

After some searching I found the parameters I needed: https://gist.github.com/luetkemj/2023628 (on Line 231) //////Search Parameter //http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter ‘s’ => $s, //(string) – Passes along the query string variable from a search. For example usage see: http://www.wprecipes.com/how-to-display-the-number-of-results-in-wordpress-search ‘exact’ => true, //(bool) – flag to make it only match whole titles/posts – Default value is false. For more … Read more

Concatenate site_url and string doesn’t work

Without knowing exactly what you are trying to do, it seems you want to append query variables to the URL. WordPress has methods for handling that properly, without manual string concatenation. Look at the documentation for add_query_arg() for details: https://developer.wordpress.org/reference/functions/add_query_arg/ You can rebuild the URL and append query variables to the URL query by using … Read more