83

query_posts - You should never ever use query_posts. Apart from what @Rarst has said, the really big issue with query_posts is, it breaks the main query object ( stored in $wp_query ). A lot of plugins and custom code relies on the main query object, so breaking the main query object means that you are breaking the functionalities of plugins and custom code. ...


69

Your tax query is incorrect, field should be the field you want to query on: term_id, name, or slug - $posts_array = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'fabric_building', 'tax_query' => array( array( 'taxonomy' => 'fabric_building_types', 'field' => '...


22

I'm a bit confused. If you want to get onlya element from the posts array you can get it like this: reset($current_user_posts) - first post end($current_user_posts) - lat post But if you want to get just one post with the get_posts() you can use the posts_per_page argument to limit the results. $args = array( 'author' => $current_user->...


18

tax_query takes an array of tax query arguments arrays (it takes an array of arrays) but you are using only single array. The correct code is as following. $uposts = get_posts( array( 'post_type' => 'product', 'numberposts' => -1, 'tax_query' => array( array( 'taxonomy' => $cat->taxonomy,...


17

Late answer as the main answer uses query_posts(), which should never be done. Use a filter Use the pre_get_posts filter and just set the portfolio post type for the main query. Use Conditional Tags to determine where you want to have this filter. Quick Example <?php defined( 'ABSPATH' ) OR exit; /* Plugin Name: (#6417) "Portfolio" post type in query *...


17

There is simply no need to use query_posts(). All it does is instantiates a new WP_Query object and reassigns that new object to global wp_query. For reference, the following is that actual query_posts() function. function query_posts($query) { $GLOBALS['wp_query'] = new WP_Query(); return $GLOBALS['wp_query']->query($query); } ...


17

Template tag functions rely on global variables to access post being processed and retrieve data from it or related to it. Main variable of them is $post which holds post object itself. In your example it's not explicit, but what is happening is that your loop assigns data to $post, if name for iteration wasn't $post you would need to do that explicitly (...


16

Make sure that you use wp_reset_query() after using query_posts() because it will affect other query result also.


16

First, you should always use the WordPress AJAX methods, not a custom function for that. See AJAX in Plugins in the Codex. With that practice in mind, you can set up your request like this. Change the AJAX URL to <?php echo admin_url('admin-ajax.php'); ?> and add the 'action': key with the value of the specific WordPress function name that you want ...


15

In all probability you are using a custom taxonomy, and not the build-in category taxonomy. If this is the case, then the category parameters won't work. You will need a tax_query to query posts from a specific term. (Remember, get_posts uses WP_Query, so you can pass any parameter from WP_Query to get_posts) $args = [ 'post_type' => 'product', '...


12

Try this... $posts = get_posts( array( "orderby"=> "slug", "order" => "ASC", "post_type" => "my-custom-post-type", "posts_per_page" => -1, "fields" => "ids", "meta_query" => array( array( "key" => "ams_park_id", "value" => get_the_ID(), ...


11

Then you should see a link "Trash" on the top of pages/posts listing section. Click on that link which will take you the section where all the trashed pages/posts are listed. Select the pages/posts using the checkboxes against them. Select Restore from Bulk Actions dropdown, then hit the Apply button. Now go to pages/posts listing section and you should ...


11

Several reasons 1. Filters and Actions By using the standard loop, you execute various filters and actions that plugins rely on. Additionally, you set up the_post correctly, allowing functions such as the_content etc to work correctly. Some filters can even insert "posts" into the loop 2. Memory Efficiency By fetching all the posts as an array, you're ...


10

I modify WordPress query from functions.php: //unfortunately, "IS_PAGE" condition doesn't work in pre_get_posts (it's WORDPRESS behaviour) //so you can use `add_filter('posts_where', ....);` OR modify "PAGE" query directly into template file add_action( 'pre_get_posts', 'myFunction' ); function myFunction($query) { if ( ! is_admin() && $...


10

As documented in the codex you could use the following $query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) ); //post has to be in category with ID 2 AND 6


10

You can use 'posts_orderby' filter to change the SQL performed. Note that: using get_posts() you need to set 'suppress_filters' argument of false for the filter to be performed if you don't explicitly set 'post_status' you'll get only published posts (so no much to order) Code sample: $filter = function() { return 'post_status ASC'; }; add_filter('...


10

You can get that value with get_option('posts_per_page');


9

query_posts(): might be used in one and only case if you need to modify main query . It sets a lot of global variables; get_posts(): it is very similar in mechanics and accepts same arguments, but returns array of posts WP_Query:  you can create and work with own object of it. Bit more complex, less restrictions, it is safe to use anywhere.


9

the_posts_navigation() is simply a wrapper function for get_the_posts_navigation() which issimply a wrapper function for paginate_links. The first two functions uses the the same exact parameters that is being used by paginate_links and actually passes it to the latter function as well get_the_posts_navigation() and the_posts_navigation() is good new ...


9

This is directly related to, and a consequence of WordPress.com VIP At VIP, we deal with sites that range in the hundreds of millions of page views per week. As a result, situations that can slow down your site are much more noticeable at that scale than on a small shared host, but this still impacts a site with moderate traffic in the region of 100 or so ...


8

I think this is the fastest way to return the results of a get_posts in a defined order. And besides that, it's a native solution, without hacks <?php $posts_order = array('1,3,8,4,12'); $args = array( 'post__in' => $posts_order, 'orderby' => 'post__in' ); get_posts( $args ); ?>


8

global post; does not work with setup_postdata($post); if you want to use the the_title() family of commands etc. It's in https://codex.wordpress.org/Function_Reference/setup_postdata Instead use // global $post; setup_postdata($post_object); //don't do this! setup_postdata( $GLOBALS['post'] =& $post_object ); ...also make sure your $post_object is a ...


8

global $current_user; $args = array( 'author' => $current_user->ID, 'orderby' => 'post_date', 'order' => 'ASC', 'posts_per_page' => -1 // no limit ); $current_user_posts = get_posts( $args ); $total = count($current_user_posts); and just loop the current user posts


7

Just to outline some improvements to the accepted answer since WordPress evolved over the time and some things are different now (five years later): pre_get_posts is a filter, for altering any query. It is most often used to alter only the 'main query': Actually is an action hook. Not a filter, and it will affect any query. The main query appears in ...


7

As of 3.7 you can use date_query http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters So it would look like: $args = array( 'posts_per_page' => 5, 'post_type' => 'post', 'post_status' => 'publish', 'date_query' => array( 'after' => date('Y-m-d', strtotime('-30 days')) ), '...


7

A WP_Query where 'tax_query' has all tag terms and operator 'NOT IN': $tags = get_terms('post_tag', array('fields'=>'ids') ); $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'post_tag', 'field' => 'id', 'terms' => $tags, 'operator' => 'NOT IN' ...


7

get_comments accepts an array of arguments, you are passing an integer. If you want to retrieve all comments for a post use: get_comments( array('post_id' => $post->ID, 'status' => 'approve') ); To get an already formatted comment list, is easier use the wp_list_comments() function, instead of another foreach cycle (code from codex): echo '<...


7

When WordPress include the category.php (just like any other template) the main query (i.e. the query triggered by the url) is already ran: it means that a query on database was already fired and this is the reason why in a template you can run the Loop without calling a query. If you call get_posts you ran another query to database, that is completely ...


7

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 ...


7

Why not use WP_Query instead. Like this. $args = array( 'post__not_in' => $inclPages, 'post_type' => array( 'page', 'project' ) 'orderby' => 'menu_order' ); $pages = new WP_Query ( $args ); WP_Query accepts wide range of parameters. And also post_type array.


Only top voted, non community-wiki answers of a minimum length are eligible