Questions tagged [query]
A generic term referring to the process of retrieving information from a database.
149
questions
439
votes
7answers
168k 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.
They all do similar things, so when should I use one over the others?
135
votes
5answers
123k 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
...
44
votes
5answers
31k 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', ...
23
votes
1answer
1k views
Should we trust the post globals?
@toscho left a comment to this answer which had me thinking again. How much trust should we have in the global scope, specially regarding the post globals like $post?
So what? The global variable ...
32
votes
5answers
166k views
How to limit the number of posts that WP_Query gets?
I have been researching on Google and WPSE and the only thing I see repeatedly is to use showposts, that is deprecated.
I am familiar with WP_Query, and I thought that if I set posts_per_page to my ...
22
votes
5answers
29k 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 like ...
22
votes
5answers
51k views
How can I create a meta_query with an array as meta_field?
Here are the args for my query :
$args = array(
'post_type' => 'news',
'meta_query' => array(
array(
'key' => 'topics',
'value' => 'sports',
...
4
votes
2answers
29k views
How do I search events between two set dates inside WP?
I am building a WP site with an events feature. The events page was made with custom post types.
I want to make it possible for people to search/ filter events by dates. For example, they can search ...
2
votes
1answer
2k views
Show only oldest post by author
I am working with a heavily customized install of WP. I've set up a specific post type, taxonomy, and roles that gets to use just that post type.
Ideally, I'd like to restrict members of that role ...
0
votes
1answer
2k views
Order posts by tags count?
I am trying to order posts by tag count. I want to order posts with no tags first and posts with highest count last.
I tried and got posts with no tags only.
$tags = get_tags();
$tag_ids = ...
17
votes
2answers
37k views
Query multiple custom post types in single loop
I'm aware there are several other posts that cover similar ground to what I'm about to ask.
I have three custom post types running, in addition to 'posts'. I want to run a loop that pulls all posts ...
117
votes
4answers
199k 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?
8
votes
2answers
19k views
WordPress Paginate $wpdb->get_results
Due to a complex multisite config, I have a query which combines the posts from two blogs and I would like to paginate the results. I am grateful for any help. I have posted my query.
$...
5
votes
2answers
3k 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 ...
15
votes
6answers
33k 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 ...
3
votes
2answers
20k views
Make a WP Query search match exactly the search term
I'm using WP Query and the results coming back when using the search term aren't working as I wish. For example searching for 'art' returns also results for words containing 'art' such as 'part'.
How ...
7
votes
2answers
12k 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 ...
7
votes
2answers
3k views
WP_Comment_Query pagination, delving into the unknown
I am aiming to pagination WP_Comment_Query(). It appears this is either taboo or there is no viable information about it online, not anything.
Why? Is it possible? If so, how?
8
votes
3answers
1k views
Remove the Homepage Query
I have a homepage displaying the home.php template, containing 2 sidebars with widgets in them.
The main query still pulls in the standard 10 posts, but since I'm not displaying these, I'd like to ...
2
votes
1answer
8k views
How to get the posts published in last two days using WP_Query?
I am trying to loop through all the post to get the top most shared posts on social network. I want to use the date_query parameter in the WP_query to get the posts of last two days , last 5 days, ...
0
votes
2answers
6k views
How to get my loop to pull posts into three columns
I have my page template pulling one specific category & pulling the featured image & title into 3 different columns on my site. The problem is they all pull the CSS class .proj-left how can I ...
5
votes
2answers
5k views
When/why does '$query->get( 'tax_query' );' return empty?
First the code:
function itsme_better_editions( $query ) {
if ( $query->is_category() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post' ) );
...
5
votes
1answer
1k 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 ...
5
votes
1answer
6k 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 ...
4
votes
2answers
1k views
How to order WP_User_Query results to match the order of an array of user IDs?
I'm using WP_User_Query to get a list of users from my database. I'm showing specific users only via use of the include parameter. For example:
$args = array(
'include' => array( 1, 3, 5, 99, ...
3
votes
4answers
22k views
Sort posts by category name and title
Trying to figure out how I can output my posts based on category title (aāz) and secondly, the title of the posts within the category:
A CATEGORY
ā A post beginning with a
ā Because I want to be ...
3
votes
2answers
4k views
Get posts by meta data OR title
I have a working query that calls all my posts that have a certain meta value for one of two meta keys:
$term = filter_var( $_GET['term'] );
$query = get_posts( [
'post_type' => 'some_cpt_name'...
1
vote
2answers
421 views
I have over 4000 posts, will querying some of them cause performance issues?
I am wondering if querying posts like this:
<?php $args = array('numberposts' => 19, 'orderby' => 'rand' );
$posts = get_posts( $args );
foreach($posts as $post) {
$...
1
vote
0answers
1k views
SQL query equivalent to WP User Query
I have the following WP user query code:
//Get all users sorted by whatever
$sorted_users = new WP_User_Query(array(
'orderby' => 'meta_value',
'meta_key' => 'satisfaction_rating',
'...
1
vote
1answer
2k views
Filter all queries with a specific taxonomy
Is there any way to filter all post queries and add a specific taxonomy? Case in point, I have a taxonomy object of "Region" with terms like "Global", "Americas", "Europe", etc. (Users designate ...
0
votes
2answers
528 views
Duplicate posts
I'm currently working on a Magazine style website and on the main page I have three different sections where I display the latest news. Slider, latest news and more news.
The problem is I can't ...
0
votes
1answer
293 views
How do I sort multiples pages?
I want to use a dropdown to sort a list of posts in WordPress. The results will display on multiple pages. So how do I pass the query on to the second, third, fourth, etc. pages?
Here's an example of ...
3
votes
4answers
10k views
wp_dropdown_categories with multiple select
in the search box I would like visitors to be able to search for more than one location (houses). The code right now is:
<div class="col-md-3 col-sm-6 col-xs-12">
<label for="...
1
vote
2answers
117 views
How to display lastest post date in the homepage?
I'm looking forward to display, somewhere in the homepage of a WP site, a legend saying:
Last updated: XX/XX
by grabbing the date from the most recent post, but not showing any of its content, ...
1
vote
1answer
18k views
How to execute SQL SELECT query and see the results?
I want to run a SELECT query on the Wordpress database and get the results on the screen.
Any widget which does it?
1
vote
1answer
173 views
Query the post which have a thumbnail
I've got a highlight section on the header of my website which shows the title and the thumbnail of the last X posts having a thumbnail.
Is there any custom query which allows me to retrieve only the ...
1
vote
2answers
1k views
Looping through posts per category gives same posts for each category
For a specific section on my website I loop through some categories and get the three latest posts in that categorie and list them. Like so:
<?php $categories = get_categories(array('exclude' =>...
1
vote
1answer
1k views
Add <link rel=“next”> and <link rel=“prev”> in the header while looping over custom query in page template
I have a custom page template defined and working properly.
On this specific page, I want to list a lot of links, and paginate them, to display 100 links per page.
Here is the code in my template ...
0
votes
1answer
9k views
How to order posts by modified date without using 'query_posts'?
I have an example basic wordpress theme template here for you. First, please take a quick look at it to get an idea.
<?php
get_header(); ?>
<div id="primary">
<div id="...
0
votes
2answers
4k views
Using dynamic conditions in 'posts_where' filter
I have the following code which gives be posts that are published in the last 100 days
function smbd_cats_by_days ($where = '') {
$where .= " AND post_date < '" . date('y-m-d', strtotime("-...
0
votes
1answer
260 views
How to show posts rank based on custom field value
I have total 100 posts, 10 posts in each page on homepage with paginatin. Here is my code
$args = [
'posts_per_page' => 100,
'fields' => 'ids',
// Add additional args here
];
$...
0
votes
3answers
8k views
Custom $wpdb Query for Custom Post Type by Category
I am using the following code to output a list of items in my 'documents' Custom Post type by Year and Month.
<ul>
<?php
$post_type = 'documents';
global $wpdb;
/**/
$years = $wpdb->...
-1
votes
1answer
134 views
Highlighting Sub topic in a post?
Have seen this sub topics highlighter in many blogs. It gives a special attraction to a sub topics used in the post.
Is this a plugin or part of code? How can I add this?
http://imgur.com/UbGpz7j
25
votes
7answers
147k 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 find/...
25
votes
3answers
19k 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 ...
24
votes
4answers
87k 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 $wpdb->...
23
votes
4answers
10k 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 ...
4
votes
3answers
19k views
how to get a different html for odd/even posts?
query posts
if posts exist
then begin the loop
if post is even: <h1>title</h1><p>content</p>
if post is odd: <div>its image</div>
...
22
votes
1answer
17k views
order by numeric value for meta value
I have read countless posts on ordering data by numeric value ( as opposed to string - it i have a numeric meta value and it gets ordered like this 1 11 12 2 233 etc as opposed to the way i want it
I ...
1
vote
2answers
6k views
wp_query orderby title and meta key value (WP3.1)
I have a taxonomy wp_query and i would like to order the list by title and by meta value (numeric value)
Have a meta value Interesting = 1 or 0 in the posts
Not so interesting posts would be at the ...