Articles of wp query

How to display the featured image for each post?

I have used the following code to display the featured image for each post, but nothing is showing: <div class=”thumbnail-img”> <?php $lastBlog = new WP_Query(‘type=post&posts_per_page=2&offset=1’); if ($lastBlog->has_post_thumbnail()) { while($lastBlog->has_post_thumbnail()) { $lastBlog->the_post_thumbnail(); } ?> <?php get_template_part(‘content-image’, get_the_post_thumbnail()); } ?> </div> <br> <?php if( $lastBlog->have_posts()): while($lastBlog->have_posts()): $lastBlog->the_post(); ?> <?php get_template_part(‘content-title’, get_post_format()); ?> <?php endwhile; endif; wp_reset_postdata(); ?> […]

Create a WP_Query where if the first value of the first row is equal to the second compare other value

I want to create a query of a custom post type where the order is by a custom field and I want if the custom fields are equal to compare a second custom field. $args = array ( ‘post_type’ => array( ‘sn_dr_ia’ ), ‘orderby’ => ‘meta_value_num’, ‘meta_key’ => ‘thePrice’, ); These are my arguments and […]

Update query for wp_posts and wp_postmeta

I had been assuming that ID in wp_posts is the primary key and that post_id in wp_postmeta is the foreign key but there is no relationship between them. How does these two tables relate each other? There is a meta_key and meta_value column where meta_key data are _sku, _price, and _stock. How can I use […]

Excluding posts with certain value of meta_key keeping posts without that meta_key

There are solutions for this problem, but what I could found, all of them are from long ago. So I am looking for a solution (if there is any) considering recent updates. I have a meta key wpse_city, which is a radio button with several values. Now I want to exclude posts with wpse_city = […]

Create an archive page for custom post type with custom taxonomy

I have a custom post type called ‘news’ with a custom taxonomy called ‘news category’. I’d like to create a wp query to return all ‘news’ posts of a certain ‘news category’ for example ‘press’. Is there a way to write this that is universal so I don’t need to make one for each ‘news […]

Pagination in html table is not working

I have a custom page template and I found a a PHP snippet that create a pagination in html table. But the code is not working properly. UPDATE: I change the code. But still the code is not working correctly, when I click on the 2nd page, the 2nd page displayed a blank page only. […]

var_dump of WP_Query object is not empty, while .have_posts() return false

I’m writing a plugin for WooCommerce. somewhere in my plugin I check that a query has post or not, but all the time myWPQUERYobject->have_posts() returns false! here is var_dump of the object: object(WP_Query)#17793 (48) { [“query”]=> array(8) { [“post_type”]=> string(10) “shop_order” [“post__in”]=> array(21) { [0]=> string(5) “12501” [1]=> string(5) “12502” [2]=> string(5) “12503” [3]=> string(5) […]

how to get posts ids inside pre_get_posts filter?

I’m trying to do these one: On the home (front page) i have multiple posts loops. I need posts to not repeat across these loops ( i.e if post was shown in 1-st loop it should never appear in 2-nd or 3-rd and so on ). I’v come up with these ( lower ), but […]

Using WP_Query and WP_Meta_Query Outside of WordPress

I’m trying to figure out how I can utilize WP_Query and WP_Meta_Query outside of the WordPress install, for server Cron jobs. I’ve tried including wp-load.php but this also brings in plugins like WooCommerce into the mix and I’m getting all kinds of header warnings, etc. I just need to be interacting with the database, not […]

query post by author gender

I’m using utimate member plugin on my website. for each member (author), I have a field to store the member gender. (Homme / Femme). the data is stored in my database, inside wp_usermeta. meta key is “gender” and meta value is either “Homme”, or “Femme”. I’m trying to write a wp-query to display all posts […]