Articles of wp query

Search by Hyphen

I’m currently running WordPress on version 4.6.1 and I’m attempting to search for posts that contains the character – (hyphen). However, the search parameter is taking my hyphen for a negation. From the WP_Query documentation: Prepending a term with a hyphen will exclude posts matching that term. Eg, ‘pillow -sofa’ will return posts containing ‘pillow’ […]

WP_Query: Why is sticky post not first item in loop?

I’ve got a custom WP_Query loop based of meta_value variables: $meta_cat = get_sub_field(‘category’); $posts = new WP_Query( array( ‘cat’ => $meta_cat, ‘posts_per_page’ => get_sub_field(‘recent_ppp’), ) ); But somehow when I’ll make a post sticky it isn’t shown as the first item in the while loop. I know ignore_sticky_posts is default set to false so I […]

Get current post id in functions.php

I have a wp_localize_script() function in my functions.php where I need to push the current post ID of the current post to a jQuery file. So I was wondering how you can get the current post ID of a post in my functions.php. These are both not working: global $post; $post_id = $post->ID; global $wp_query; […]

The next_posts_link() works only with original $wp_query

I’ve got custom template that I want to display paged blog posts. This is the beginning of my file: $wp_query = new WP_Query($args); if($wp_query->have_posts()){ while($wp_query->have_posts()){ $wp_query->the_post(); //something… <?php next_posts_link(‘Older Entries’); ?> <?php previous_posts_link(‘Newer Entries’); ?> It works fine – it displays OLDER and NEWER links when it should. On the first page it will display […]

How do I query for posts by partial meta key?

I have a function that stores the “like” status for a post as post meta. I want to associate that “like” with the user that liked it, so I setup a custom field called “like_status_{user_id}” (where {user_id} is the id of the currently logged in user) which I store as a 0 or 1. So […]

WP_Query: get 3 random posts from 10 latest

Working on a site that has a lot of posts, I need to display 3 posts from a particular category, but all of them need to be from the latest 10 published on the site. I can either grab 3 completely random posts (which tends to pull posts that are very old) or grab 10 […]

query to find “parent” posts of CPT A while filtering “child” posts of CPT B

Lets say we have two CPTs, one of cities, and another of restaurants. Each restaurant is located in a city (duh) and serves different types of foods, different hours, different prices, etc. Each restaurant has the post ID of the city in a “city” meta field, and the rest of the properties are in other […]

Custom Taxonomy WP_Query for All Terms in a Taxonomy?

Is there an easy way to query for any posts that is tagged with any term from a particular taxonomy? I know this technique: $custom_taxonomy_query = new WP_Query( array( ‘taxonomy_name’ => ‘term_slug’, ) ); But I would like to either pass a wildcard in place of term_slug, or perhaps just an empty string. Then that […]

Using WordPress to make a “Product Search” type navigation drilldown

This is somewhat of a follow-up to a question I asked some time ago: https://wordpress.stackexchange.com/questions/11088/trying-to-design-nice-category-drilldown-with-one-held-taxonomy-and-a-repeatabl I’ve sort of got a better idea of what I want, but only a vague idea of how to accomplish it and I was hoping to get some feedback. I’m going to be using wordpress as a CMS. Instead of […]

Count the number of images uploded on the website

I mention “full website” because I don’t need the number of attachments on one post (as answered in many questions). I basically need a function that returns the number of uploaded images (attached and unattached) on the website, excluding non-image files. I’ve done some research, but there isn’t any direct function that counts only the […]