Questions tagged [wp-query]

"wp-query" ambiguously refers to one of two things. 1) WP_Query is a class native to WordPress which allows users to query the posts database using a number of criteria. 2) $wp_query is the global variable that holds the main query object prepared by WordPress based on a given request.

1,434 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
4
votes
1answer
4k views

Add indexing to meta_value in wp_postmeta

Is it poossible to add index to the meta_value column in the wp_postmeta table in order to make faster queries in Wordpress? I have about 5 million rows in the wp_postmeta table and my queries takes ...
4
votes
1answer
676 views

How can I use order_by to order by two meta_keys without excluding posts that don't have those keys initialized?

I have two meta_keys on a custom post type. I want to be able to query all of these posts, and order them by the two meta_key, one taking precedence over the other. I.e. I have one meta_key called ...
4
votes
2answers
3k views

meta_query where value is equal to given value

I currently have this custom query which I am trying to filter the product (paintings) by artist. $artistID = $_GET['artist_id']; $args = array( ...
4
votes
1answer
59 views

Getting wrong relationship value in $args in wp_Query?

I am trying to fetch data from the database for my custom plugin and post type. my query arguments should be like: $args = array( 'post_type' => 'products', 'post_status'=> 'publish', ...
4
votes
1answer
1k views

Sorting posts by custom date fields (non standard date format)

I've inherited a website with a post type courses that has a custom field 'date-start', a Date Picker. Unfortunately the date start is stored as 'ddmmyy' and the ACF documentation implies that this ...
4
votes
1answer
158 views

Get page content by category or tag

I'm trying to sort of emulate the Views behaviour in Drupal with WordPress, but doing it in theme templates. Now, I have pages that are assembled by using wp_query in the template to get posts ...
3
votes
1answer
84 views

Adding a span when custom post type is updated

What I'm trying to do is have a span tag appear on the top of the post type whenever the post is updated. The way the page is setup, there is a query of different post types that list jobs currently ...
3
votes
0answers
863 views

Show all parents and children in custom post type in right order

I would like to show all posts from a custom post type on one page with the ordering exactly like in the backend. I get the right ordering with wp_list_pages, but of course without the content. ...
3
votes
0answers
60 views

Select menu should display categories and the item's depth

I have a select menu of a custom post type in the header.php: <?php $selectloop = new WP_Query( array( 'post_type' => 'products', 'posts_per_page' => -1 ) ); echo '<form>&...
3
votes
1answer
133 views

Custom Taxonomy and tax_query Issue?

I am working on Plugin development and my plugin name is plugindev.I have a custom post type called team.I have a custom taxonomy Team_Category which is being registered by this code /****************...
3
votes
1answer
257 views

Generate a tabbed submenu — from taxonomy term or submenu item — with sample content

I'm looking to generate a tabbed submenu from taxonomy term or submenu item and include sample content (three items per term/submenuu item). The styling I can do, the problem is the html generation. ...
3
votes
1answer
445 views

Query Custom Meta Value with Increment

I have a Query searching for a meta value I have added to my posts (meta-select). I have it feeding out in a loop with 4 results. Let's say I want this loop to start with the meta value "2" and work ...
3
votes
2answers
1k views

How do I order pages and categories by ID or name in the same query?

I would like to query most of my pages and categories on the homepage. So far I am able to get the pages and category (posts), but I cannot order the list the way I want to. I want to be able to get a ...
3
votes
1answer
9k views

wp_query->max_num_pages always returns 0 on custom post type

Hi there I am developing a theme where I have to load some posts with ajax, in my index page everything works properly, the problem is when I need to use it on custom post type, I pass the values for ...
3
votes
2answers
514 views

How to target the default Recent Posts and Recent Comments widgets with pre_get_posts?

I added the following to my functions.php: add_action('pre_get_posts', 'keyl_get_emp_posts'); function keyl_get_emp_posts($query) { if ($query->is_main_query()) $query->set('...
3
votes
3answers
883 views

new WP_Query() - what is the earliest valid hook?

Specifically, is it valid to run a new WP_Query() during the plugins_loaded action?
3
votes
2answers
429 views

Extending WP_Query — Optimise SQL query

I'm storing posts, a user follows in a custom table that has the columns id, post_id and user_id. To fetch posts that a user follows I have extended the WP_Query as follows: class ...
2
votes
0answers
48 views

Multiple WP_Query loops with Pagination Not Working

Please can anyone help me out here? I'm really stucked with this code. I want to create a multi post loop on my home page with each loop having its own pagination. The first loop is for recent post, ...
2
votes
0answers
55 views

Can an array be used as a meta_query value?

I'm trying to query a custom post type (courses) by grade using WP_Query and Advanced Custom Fields (ACF). Grades (checkboxes) are organized by term (groups). Four grades per term, four terms per ...
2
votes
1answer
160 views

meta_query works locally but not on live server

I'm filtering posts by author's last name initials. On my local server, the query runs beautifully, but when I push live, it doesn't. It doesn't find anything. Does it have anything to do with the way ...
2
votes
1answer
548 views

Working Bootstrap Carousel Conversion to WP - Technical Questions

I have coded a working Bootstrap carousel, it needs to be converted to WP. It's a small chunk of the site, but as a Wordpress newbie, am stuck in understanding the technical issue. So here's what I ...
2
votes
1answer
214 views

Ordering Posts Type A by Custom Fields of related Post Type B

I'm not sure if this is even possible (also posted to ACF forum https://support.advancedcustomfields.com/forums/topic/ordering-by-custom-field-of-relationship-field-object/#post-62454) I have two ...
2
votes
0answers
284 views

Woocommerce query specific product from specific category

I'm trying to create a shortcode that will show a woocommerce product via just a shortcode. I want the shortcode to take 1 parameter that is either the ID or slug of the actual product. Preferably ...
2
votes
0answers
34 views

Get the Plugin Which Triggered a MySql Query in Wordpress?

I am currently working on this plugin, which is similar to Query Monitor in which i Have displayed all the Details present in the $wpdb object. Now i need to find which plugin is Triggering the Query. ...
2
votes
0answers
156 views

Wordpress posts per page WP_Query

I'm trying to limit the posts per page using WP_Query. Please see the code below and see why it doesn't work for me: public function widget( $args, $instance ) { // outputs the content of ...
2
votes
1answer
47 views

Difficulty with rewrite rules

I can't work out how to combine a rewrite rule and a custom query var. I have created a custom 'edition' template, which calls an external API to populate a table with details of a particular book. ...
2
votes
0answers
76 views

WP_Query compare with dynamic values

I need get a post only if publish_date is equal to modified date in this case the post is not modified! So i need post NOT MODIFIED!: My query is: $lastupdated_args = array( 'paged' => $...
2
votes
0answers
407 views

Search query - exact post title match

I have a bunch of posts titled something like this... English Language 2015 English Language 2016 English Language 2017 English Language 2018 And on each one of these posts I have a dropdown select ...
2
votes
0answers
808 views

WP Rest API max limit include parameter?

I'm using the rest API to get only selected posts though Rest API. I have used include=<ids> parameter. I have send the API request like: http://example.com/wp-json/wp/v2/posts?include=11,12 ...
2
votes
0answers
30 views

WP_Query mixe post and page attributes

I am working on a website developed by someone else (it's a bit messy). Now I have to make a small widget to let the users search for some of the content that has been created. I need to be able to ...
2
votes
0answers
85 views

Adding multiple post queries with parent and children to page - Best Way

So I am interested in allowing a custom menu to show "in three places: Menu area, Homepage body, and footer" based on the posttype "page" table list or parent and children entries and not using the WP ...
2
votes
1answer
713 views

WP_Query min and max values

I'm building a houses class and I want to filter these houses with a query. But don't get any results: Example house: Min price: 10 Max price (field not required): empty I've a price filter ...
2
votes
1answer
2k views

Display related products with custom output

I'm trying to display the related products with my custom html output, unfortunately i'm kind of lost on how to do this. The WP_Query below displays the products that are in stock, what i want is to ...
2
votes
0answers
50 views

Multiple meta queries but arrange by specific meta value order

Im working on a function which gets me lawyers based on criteria. The function works fine. Recently I added a meta key called 'is_endorsed'. The value can be either '1' or '0'. I need to bring ...
2
votes
1answer
43 views

Hiding posts in a list from specified categories

Question How can I modify this code to not show posts from new categories that we just added. I searched other similar answers on this site [such as: Exclude category on blog list page ] but not ...
2
votes
0answers
52 views

How to show min 1 post from specific categories/tax WP_Query?

I have custom query that shows specific number of posts from specific categories/tax. Currently I limited the query to show posts from 2 categories , 3 and 4. Issue I have is that the query can be ...
2
votes
0answers
405 views

Custom pagination with WP_Query generating 404 error

I'm using WP_Query to generate a custom loop on my blog page. My current blog page is assigned to a page called "Blog" and this page is using the default template file (index.php). Here's a piece of ...
2
votes
0answers
56 views

Interupting the loop with extra divs to display data, and how to get it to work responsively

Hope you're good, wondering if anyone can help, or at least point me in the right direction. I've got a page with images of people on it(approx 30 people) The objective is that when a user clicks ...
2
votes
0answers
1k views

WP_Query & AJAX

When calling wp_query via AJAX and passing a category ID and search parameter, the query does not return any posts. On page load, the query runs fine, which runs the same query. If I add these ...
2
votes
1answer
47 views

how to get the id for a post from within a post and return post meta for it

I need to query for a post outside of another post: I am using a hook to import a page builder template and I need a query in relationship to the post it is hooked into. I not a huge fan of builders ...
2
votes
1answer
405 views

Revolution Slider Orderby Two Custom Fields

I have create Revolution Slider which load the custom post type, Events. There are two custom fields, Event Start Date and Event End Date. I would like to display order by Start and End date. Because ...
2
votes
1answer
520 views

How to count data records in wordpress which have same meta_value in wp_postmeta table?

I want to get number or count of same meta_value of meta_key == 'parent_id'.How can i get it? What should be the wordpress query for counting records in same table? //set the meta_key to the ...
2
votes
0answers
212 views

How do I dynamically generate args for wp_query?

I am trying to build up my search args depending on variables passed through from a form. What I want to do is add to args if values are present. For example if there are dates I have: $args = ...
2
votes
1answer
597 views

Order wp_query by calculated field

Hi I'm working on a store locator. I have a custom post "clinics" where I save clinic details (address,.., lat, lng). Each time I publish a new clinic I save post_id, lat and lng also in a custom ...
2
votes
1answer
60 views

Lists Posts from CPT that match the Title of the Page

I am trying to get all the posts associated with the Title of a page. This is what I currently have: <?php $title_to_tag_posts_query = new WP_Query( array( 'tag' => ...
2
votes
2answers
1k views

How to display custom field on homepage

I am new to custom fields thing. I am working on a movie review site. And wants to add movie story or plot field, I have successfully added the custom field named Story and its content is displaying ...
2
votes
1answer
259 views

Help with CPT template pagination

i created a themes CPT using the following code function pp_themes_post_type() { $args = array( 'public' => true, 'label' => 'ProfilePress Themes', '...
2
votes
1answer
1k views

How to get a category in a list item class

I am need to know how would I get the category to be dynamically input in a WP_Query This is the code I am using: <li class="<?php the_category(' ')?>"> but the output is breaking. ...
2
votes
0answers
848 views

tax_query OR with empty result returns all posts

I'm running a WP_Query with a tax_query with two tax_query statements with an OR relationship. $tax_query_terms = array( array('taxonomy' => 'post_tag', ...
2
votes
1answer
220 views

How to do a query on custom taxonomies that is uncategorised?

I have a custom taxonomy and I wish to do a query for all custom posts who has no value assigned for that taxonomy (i.e. uncategorised). This is what I have so far: $uncatQuery = new WP_Query( ...