How to count posts with specific arguments

The easiest way to optimize this query would be to add ‘fields’ => ‘ids’ to the arguments. This way only ids of posts will be retrieved from DB (and usually that’s a big change). $args = [ ‘numberposts’ => -1, ‘post_type’ => ‘jobs’, ‘s’ => ‘developer’, ‘tax_query’ => [‘taxonomy’ => ‘IT’, ‘field’ => ‘slug’], ‘fields’ … Read more

Custom Taxonomy with tax_query

From the WordPress codex for wp_query: Display posts tagged with bob, under ‘people’ custom taxonomy: $args = array( ‘post_type’ => ‘post’, ‘people’ => ‘bob’ ); $query = new WP_Query( $args ); Display posts tagged with bob, under ‘people’ custom taxonomy, using tax_query: $args = array( ‘post_type’ => ‘post’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘people’, … Read more

Counting posts with argument without retrieving the posts

Here’s one thing you could do. Create a WP_Query object but only include ids: $args = array( ‘posts_per_page’ => -1, ‘post_type’ => ‘books’, ‘s’ => $s, ‘fields’ => ‘ids’, ); $query = new WP_Query( $args ); $post_counts = $query->post_count; wp_reset_postdata(); By using fields => ids you’re only returning the IDs. Also, WP_Query already does a … Read more

How to show more post at a given category?

put this code in functions.php : function change_numberposts( $query ) { // Only modify the main query if( $query->is_main_query() ){ // Home page if ( $query->is_home() ) { $query->set( ‘posts_per_page’, 6 ); } // Category 21 if ( $query->is_category( 21 ) ) { // parameter for is_category may be Category ID, Category Title, Category Slug … Read more

Getting only a specific post type with get_post?

$the_posts = get_posts(array(‘post_type’ => ‘post_type_name’)); That will get all posts from the post type called “post_type_name” so replace that with whatever custom post type you are using. I mean this in the nicest possible way, but you should probably read the documentation as this is a pretty well documented feature that isn’t too hard to … Read more

Get all pages and posts with get_pages() or get_posts()

get_pages does not work with posts or post types that is not hierarchical like pages, so if your post type is hierarchical like posts, it would not work This function can also retrieve other post types using the ‘post_type’ parameter, but the type must be hierarchical like pages, or the function will return false. If … Read more

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