Articles of wp query

Using one WP_Query object within the loop of another WP_Query object

I’m creating two different WP_Query objects, one for each of two different post types I’m using. In creating my output, I’d like to loop through one of the objects, and at the same time loop through the other object as well, so I can output pieces of them together. Here’s an example of what I’d […]

Use get_post_types to query only custom posts types

I need to query only custom posts types – that is all post types in my WP install excluding posts and pages. I have used get_post_types to build a string of all custom post types which I want to query: $args=array( ‘public’ => true, ‘exclude_from_search’ => false, ‘_builtin’ => false ); $output = ‘names’; // […]

Using new WP_Query in shortcode in a custom field causes the main post content to not display

I’ve got a custom post type of files I’m trying to list using a new WP_Query. It works fine when I add the shortcode within the main post content. However, when I add the shortcode to a custom field (which I have set to display on the page along with the main content as a […]

How can I combine meta_query queries?

I’m trying to build combined meta_query queries but they don’t work. Any ideas how I can implement queries like the example below? Thanks, Hinnerk Array ( [relation] => AND [0] => Array ( [key] => price [value] => 1000 [type] => numeric [compare] => <= ) [1] => Array ( [relation] => OR [0] => […]

Pull post meta with post_query?

I’m building a custom index loop that just uses query_posts at the moment. The issue is that inside The Loop, I have to then query for the post meta data (with get_post_meta), which I believe has to query the DB each time it’s called. Is there any way I can pull the meta data with […]

Best Practice For Querying Grandchildren?

Objective: Query and loop through a page’s third-level “grandchildren” in a way that allows pagination. Here’s the code that I’m using (pagination excluded): // Get the ID of the first generation $gen1_ids = $post->ID; // Query for second generation IDs $gen2 = $wpdb->get_col( “SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_parent IN ($gen1_ids) AND $wpdb->posts.post_type = ‘page’ […]

Prevent duplicate post from dynamic multiple loops

I was trying to list 20 recent posts, and put a post from custom post type on every 5th item. So it’s like: Post Post Post Post Post Custom Post Type Post Post Post Post Post Custom Post Type .. and so on. Here’s what I did so far. It’s working but the custom post […]

How WordPress attaches its Featured Images with posts?

I’m working on a WP_Query(), where I’m trying to fetch all the images using ‘post_type’ => attachment and ‘post_parent_in’ => array( 1688 )(I want to pass some more post_ids) along with other parameters. With these I can fetch all the attachments, but the featured image. I attached two different images into a post to check […]

Remove duplicated values from a loop

I have a loop that outputs <option> fields for a <select> group. It contains many cities’ names for many travel packs that I am publishing. The trick is that I need avoid repeated values. For instance: I have 4 travel packs departing from Chicago, 5 from Orlando, 3 from Los Angeles e 2 from New […]

Display posts if specific country

I’m running a simple query that displays some posts within a custom post type. Via each post within that post type, I’ve added an ACF True / False checkbox. If the checkbox is ticked, I’d like that post to only appear if you’re within the USA. Here’s what I have so far: <?php // Begin […]