WordPress Developer Ukraine, Kyiv
WordPress get_posts vs WP_Query vs query_posts: When to Use Each

WordPress get_posts vs WP_Query vs query_posts: When to Use Each

get_posts(), WP_Query or query_posts(): which one to use WordPress has three ways to get posts: get_posts(), new WP_Query() and query_posts(). They accept the same arguments and end up in the same SQL, but they behave differently, and one of them should practically never be used. Choosing the right one avoids broken pagination and posts that…
Modify the WordPress Main Query with pre_get_posts: 5 Common Patterns

Modify the WordPress Main Query with pre_get_posts: 5 Common Patterns

Modify the WordPress main query with pre_get_posts: 5 common patterns WordPress builds and runs the main query (the one behind the Loop on the home page, archives and search) before the template is loaded. When archive.php starts, the posts are already fetched. The pre_get_posts action fires after the query object is created but before the…
WordPress Array Helpers: wp_list_pluck, wp_list_filter, and wp_list_sort for Object Arrays

WordPress Array Helpers: wp_list_pluck, wp_list_filter, and wp_list_sort ...

wp_list_pluck(), wp_list_filter() and wp_list_sort(): array helpers in WordPress WordPress returns lists everywhere: get_posts() gives WP_Post objects, get_terms() gives WP_Term objects, get_users() gives WP_User objects. Getting one field from all of them, keeping some of them or sorting them by a field are everyday tasks, and core has three small functions for them that work with…