A generic term referring to the process of retrieving information from a database.

learn more… | top users | synonyms

209
votes
6answers
70k views

When should you use WP_Query vs query_posts() vs get_posts()?

It seems like half the tutorials in the Codex and around the blogosphere use query_posts() and half use WP_Query. What's the deal?
29
votes
5answers
51k views

How to display sql query that ran in query?

I have come across a function before that displayed the exact SQL code that was used in a loop for example, but can't remember. Can anybody tell me that function? Cheers
21
votes
1answer
14k views

How to fix pagination for custom loops?

I have added a custom/secondary query to a template file/custom page template; how can I make WordPress use my custom query for pagination, instead of using the main query loop's pagination? Addendum ...
14
votes
4answers
3k views

Show a different number of posts per page depending on context (e.g., homepage, search, archive)

In the Reading Settings, there is place to set the number of posts shown that affects the number of posts shown in all contexts. I'd like to show instead a certain number of posts on the homepage, and ...
13
votes
4answers
14k views

Pagination not working with custom loop

I've got a custom loop that I'm using to display some Real Estate listings that will be available within 60 days. I'm calling it with the following function: <?php $sixtydays = date('Y/m/d', ...
10
votes
6answers
13k views

Wp get all the sub pages of the parent using wp query

Here is my code $my_wp_query = new WP_Query(); $all_wp_pages = $my_wp_query->query(array('post_type' => 'page','post_parent'=>$parid,'orderby'=>'title','order'=>'ASC' )); It displays ...
9
votes
1answer
106 views

Should we trust the post globals

@toscho left a comment to this answer which had me thinking again. How much trust should he have in the global scope, specially regarding the post globals like $post. So what? The global variable ...
8
votes
6answers
37k views

What SQL Query to do a simple find and replace

Whenever I create a new website I first create a staging site on a subdomain like "stage.domain-name.com". After everything works correctly I export the database, open it in notepad++ and do a ...
7
votes
4answers
3k views

the_date() not working

I am using wordpress 3.2 and I did a query post like this: <?php query_posts("posts_per_page=1post=type&page=post_parent=10");?> Then I try to echo out the date of this post I queried ...
7
votes
3answers
636 views

Order posts (across the whole site) by metadata date

My custom 'Home' page is set to sort my custom posts via the custom date field with this (updated to be custom named) code in functions.php: // sort order for home page add_action('wp', ...
6
votes
4answers
16k views

Query multiple meta key values?

How to query for mutiple meta key values with the same key $querystr = " SELECT $wpdb->posts.* FROM $wpdb->posts, $wpdb->postmeta WHERE ...
6
votes
4answers
29k views

Proper way to get page content

I have to get specific page content (like page(12)) I used that : <?php $id=47; $post = get_page($id); echo $post->post_content; ?> Work nice execpt for compatibility with qtranslate ...
6
votes
2answers
7k views

Get all image from single page using this query

I'm having some trouble with this sample widget code. I want to get all images (Minus the post thumbnail) from a page called "Gallery" but for some reason this is pulling all uploaded images from the ...
5
votes
2answers
2k views

Count & Display Database Queries

I'm looking for an solution how I can count and display all queries in a WordPress site. Does anybody know, if there is an good plugin? Otherwise it would be an solution to check the queries on the ...
5
votes
6answers
5k views

How can I make wp-pagenavi work on a custom query built upon a form submission? [closed]

I've also posted this on the wordpress support forums, for scribu's wp-pagenavi plugin: http://wordpress.org/support/topic/plugin-wp-pagenavi-custom-query-form-submit-part-2?replies=1 My situation: ...
5
votes
2answers
1k views

Usage of the new “posts_clauses” filter in Wordpress 3.1?

Just noticed that 3.1 added a new filter to customize queries: posts_clauses. All I could find about it is that instead of using the separate query filters like posts_where or posts_join, you can ...
5
votes
1answer
537 views

How to organize and cache additional data associated with terms?

I'm updating a plugin of mine and am have a hard time figuring out the "best way" to build in a specific bit of functionality. The plugin allows users to associate images from their media library to ...
4
votes
3answers
2k views

Optimize Multiple Taxonomy Term MySQL Query?

(Moderator's note: Title was originally: "query/database optimisation") I'm written a function for a custom "filter" search panel that allows users to select terms from up to four custom taxonomies. ...
4
votes
1answer
621 views

is_category() in pre_get_posts strange error

I have a function that sets posts_per_page option depending on the category viewed. I discovered and error which I can't really understand: first this is how I get the error: function ...
4
votes
2answers
436 views

How to display Section for certain time

i am building a News site. I want to display 1 featured post from "prime" category for only few hours. after it should be expired or disappeared from the section. please have a look here u can see ...
4
votes
1answer
1k views

Changing the meta_query of the main query based on custom query_vars and using pre_get_posts

I've registered custom rewrite rules and query_vars to use for displaying a list of events based on ISO date format. For example when a user requests the URL, http://site.com/by-date/2013-04-04/, my ...
4
votes
1answer
154 views

How to uniquely identify queries?

Today I was thinking about how to differentiate queries, mainly to create an unique identifier. In the process I played around a bit, see below code. The main idea is to do it depending on the ...
4
votes
1answer
2k views

restrict_manage_posts not working in 3.3.1

I have noticed my custom taxonomy filters no longer work in the 3.3.1 admin as per these methods: Adding a Taxonomy Filter to Admin List for a Custom Post Type? I also discovered that to filter a ...
4
votes
1answer
622 views

Why does `get_permalink()` produces an add. DB request without $post->filter?

I'm currently working on a custom loop/query where I need to add the permalink of a post to the array of posts I'm retrieving from the DB. Sadly get_permalink() produces one additional DB request ...
4
votes
1answer
70 views

What is the most efficient way of querying posts based on visits and date for current day?

I am about to develop a piece of code for my site to display the most popular posts of the day that are X days old. I would like some help from you to achieve this the most efficient way. This is ...
4
votes
3answers
659 views

Query custom post type by custom field

General Info I used the plugin WP Types (wp-types.com) to create a custom post type called "artist". In WP Types you can also set another post type as parent post type. This is not possible by ...
4
votes
2answers
202 views

Rearranging posts based on categories

Hey everyone on Stackexchange! I'm trying to find a way to prioritize posts based on a category in a loop, which is sorted by dates. All posts are grouped under their respective dates. The date ...
3
votes
3answers
8k views

Using nextGen Gallery.. how can I call a list of all Gallery Names?

I'm trying to make a nav to access all the different galleries on a single page. Instead of hard-coding all their names out. I was curious if it was possible to make this dynamic and just call all ...
3
votes
3answers
280 views

Compare two numeric custom fields

I use Advanced Custom Fields and have a custompost-type "matches". This post-type contains two fields "goals-made" and "goals-against". I want to query wordpress to show only the matches that were ...
3
votes
1answer
2k views

WP_Query vs get_posts

I have read a number of questions on SE regarding these but I still can't figure the exact difference wrt usage. Would it be true to say that I would probably use WP_Query for the majority of ...
3
votes
2answers
108 views

How to search display_name column when using WP_User_Query

I'm using WP_User_Query to bring back a list of users. According to this article, I can focus the query on particular columns (I'm assuming in the wp_users table) using the search_columns param. My ...
3
votes
2answers
100 views

Differences between wpdb->get_results() and wpdb->query()

Who can explain me the differences between wpdb->get_results() and wpdb->query() in making generic queries to wordpress db?
3
votes
3answers
4k views

How to use a custom post type as front page?

I would like to set a site's front page to be a single post from a custom post type. I have been able to alter the request for my front page to a Custom Post Type archive with the following code ...
3
votes
2answers
885 views

Sort custom post type list table by display name of a user id stored as post meta value

I have a custom post type named domicile. Each post (domicile) has an owner (not the author). Owners are users with a custom role. I store the user id as a post meta value (dmb_owner) for the domicile ...
3
votes
2answers
4k views

How to query for most viewed posts and show top 5

Here is how I am getting the views for one post: function getPostViews($postID){ $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); ...
3
votes
3answers
2k views

Reversing the order of posts AFTER the query is performed

I'm am displaying the last 5 posts of a custom post type 'show'. This gives me the latest post first. <?php $args = array( 'post_type' => 'show', 'posts_per_page' => 5, 'order' ...
3
votes
2answers
2k views

Create pagination and order according to alphabet

is it possible to create an alphabet pagination? as well as alphabet order of a query post??? I can't a solution on this based on the regular queries. I would appreciate any help, thanks in ...
3
votes
1answer
2k views

Using WP_Query - how to display something IF there are no results

I've used WP_Query to query and display events. In some cases, though, there are no events to be displayed and I'd like to display a friendly message when that happens instead of displaying nothing - ...
3
votes
2answers
109 views

How to Optimize WP site for millions of posts

I'm working on a website for a company that will very likely be creating millions of posts via a custom post type. They're prayers, so basically the user on the front end just submits a short phrase ...
3
votes
2answers
258 views

Identify which loop you are hooking into; primary or secondary?

What is the most efficient way to determine which loop I'm in? I have a few plugins that alter the query by hooking into various parts of WP_Query::get_posts(), through the usual suspects, ie ...
3
votes
2answers
136 views

Query by one meta_key and sort by another (possibly NULL value)

I want to query by 1 meta key, but sort by another key that may or may not exist, and then by post date So the end result would be something like: Meta Key A / Date 1 Meta Key A / Date 2 Meta Key B ...
3
votes
1answer
2k views

Custom Search Query

I'd like to set up a custom search page that does the following: User checks off several items in a form that he'd like to see returned in his search (essentially choosing from a list of tags). ...
3
votes
1answer
2k views

Using query_vars filter

I am trying to link out from the WP admin to view posts in a specific way. The url structure for this is example.com/post123/?my-preview=456 /post123/ is the regular permalink for a post. The ...
3
votes
1answer
5k views

How to get comments by post ID?

I have this custom post query to list all the posts within a specific category. For example I have this: $args = array('cat' => 'home','post_type' => 'post')); $post_obj = new WP_Query($args); ...
3
votes
1answer
3k views

Meta Query with AND & OR?

I'm trying to do some filtering for some posts in WP_Query, but I can't seem to get the right condition. Basically what I wanna do, is get posts that have the custom field event_data (sorted by this ...
3
votes
2answers
535 views

Sort X categories by last update and show image

I want to display X categories, and sort them by last update. I also want to grab the image of the latest post with the category. <img src="<?php echo ...
3
votes
1answer
191 views

How to make an activities stream mixing posts and comments?

What I'm currently trying to make is an activities stream wich list : The posts, the comments, both mixed between them and ordered by date. My idea is to make two queries, but i don't know how to ...
3
votes
1answer
217 views

Why does wordpress still strip my query var?

Im trying to pass a query var to a custom page template with pretty permalinks. So basically i have a page called "albums". And i want to pass a query var to that page called "type". So the ...
3
votes
1answer
176 views

How to query for a week using key => value WP_Query argument notation?

I would like to be able to query for a specific year and week using WP_Query argument notation. I am using this notation as I would like the page to paginate. What's the correct "year" and "week" ...
3
votes
2answers
309 views

Very slow query

This query works, but it takes upwards of 20 seconds to run on a Quad Core dedicated server... How can I optimize this? $slides=get_posts( array('post_type' => 'any', 'numberposts' => 5, ...