Articles of wp query

Function to retrieve IDs of posts, cache results, and improve wp_query

I’m looking to optimize db performance by creating a function that retrieves IDs of posts in a certain category and caches results. So instead of having a query like: SELECT … WHERE 1=1 AND wp_posts.ID NOT IN ( SELECT tr.object_id FROM wp_term_relationships AS tr INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy […]

How to add multiple custom URL variables?

I am building a WordPress plugin that looks for different custom variables in the URL. The way I am able to achieve that right now is with this code: function add_custom_query_var( $vars){ $vars[] = “variable1”; return $vars; } function add_custom_query_var1( $vars1){ $vars1[] = “variable2”; return $vars1; } add_filter( ‘query_vars’, ‘add_custom_query_var’ ); add_filter( ‘query_vars1’, ‘add_custom_query_var1’ ); […]

What is wrong with my WP_Query Arguments?

In my single.php file I am using a nested WP_Query to show more posts, at the bottom of the page. My code for the query and nested loop are as follows: <?php $ep_args = array( ‘post_type’ => ‘post’, ‘category_not_in’ => 93, ‘orderby’ => ‘rand’, ‘posts_per_page’ => 4, ‘offset’ => 1, ); $ep_query = new WP_Query( […]

How to use global post counter in the loop?

In the Loop use this code <?php $count = $wp_query->current_post; echo $count; ?> On first page post counts from 0 to 14, but from second page post counts from 0 to 14 to. How use global counter for count post on second from 15 to 29, third from 30 to 44 …

How to use 'WP_Query' or 'query_posts' to display content in a descending order

How can I list posts in a descending order of the $json_data->data[0]->total_count values ? I want to display top shared posts descending, but I can’t really use WP_Query or query_posts the right way. require(‘../wp-blog-header.php’); query_posts(‘&showposts=-1’); while (have_posts()) : the_post(); if ( has_post_thumbnail() ) { the_post_thumbnail( array(40,40) ); } $url = get_the_permalink(); $json = file_get_contents( ‘,%20total_count,%20share_count,%20click_count,%20comment_count%20FROM%20link_stat%20WHERE%20url%20=%20%27’ […]

How to query Posts from a custom post type which contains a custom taxonomy?

I have the following: $queryResponse = new \WP_Query(array( ‘post_type’ => ‘portfolio’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘portfolio_date’, ‘field’ => ‘name’, ‘term’ => $dateSelected ) ) )); var_dump($queryResponse); $dateSelected is ‘2015’. I want: All posts from post type of portfolio with a custom taxonomy of portfolio_date (Actual name: Portfolio Date. This is what the user […]

Force WordPress to be more random

I use a random wp_query to load 3 posts from over 600 posts in total with this query. <?php $args = array( ‘post_type’ => ‘post’, ‘cat’ => 1, ‘orderby’ => ‘rand’, ‘posts_per_page’ => 3 ); $all_posts = new WP_Query( $args ); while ( $all_posts->have_posts() ) : $all_posts->the_post(); get_template_part( ‘content’ ); endwhile; ?> <?php wp_reset_postdata(); ?> […]

How to order WP_Query to group results?

I have a list of products in WooCommerce that have titles like so: Please note: The “groups” are just for myself, they are not categories etc. All products are the same type of product, just two types of products have different codes (Z and UK) “Group 1” – Unique numbers 5000 4999 4998 4997 4996 […]

How do I check if there is a tag in the title

I have many post with multiple tags ex: AMOS LEE, FOO FIGHTERS,TAYLOR SWIFT Post only have one of the tags in the title ex: “The Man Who Wants You” – Amos Lee How would I scan the_title() and go through the tags and store the matching tag in a variable? Ive tried <?php $id = […]

Using Cookie Data For WP_Query Loop

I’m trying to retrieve data set in a cookie and use this to run a query. I’ve set the cookie in functions.php like so; add_action( ‘init’, ‘resultsCookie’ ); function resultsCookie() { setcookie( ‘your-results’, ‘18,17,11,8,10,27,26’, time() + 3600, COOKIEPATH, COOKIE_DOMAIN ); } While I can echo the string, I can’t seem to get this functioning within […]