Get posts for which a custom field is not present, with get_posts

You could use NOT EXISTS as the value for comparem, you could also check for empty value if thats relevant This code only checks if hidden meta does not exists $postsForSitemap = get_posts([ ‘numberposts’ => -1, ‘orderby’ => ‘modified’, ‘post_type’ => [‘post’, ‘page’], ‘order’ => ‘DESC’, ‘meta_key’ => ‘hidden’, ‘meta_compare’ => ‘NOT EXISTS’ ]; This … Read more

How To Fix WP Query Returns Results But Shouldn’t?

Couple of things to consider here: 1.) Try adding the meta query only if the value is present: $pairedThing = get_field(‘myThing’); $args = [ ‘posts_per_page’ => 2, ‘post_type’ => ‘thing’, ]; if ( ! empty( $pairedThing ) ) { $args[‘meta_key’] = ‘identifier’; $args[‘meta_value’] = $pairedThing; } 2.) Account for posts without the meta-key set: $pairedThing … Read more

While creating Block Themes in WordPress, how can I query custom posts for an Archive view?

I found a solution for this. So for those of you who face a similar situation, here’s what I did (based on this solution : https://stackoverflow.com/questions/68871598/wordpress-query-loop-block-not-showing-custom-post-types) So basically the custom post type that I had created needed to have this argument while registering the post-type in functions.php ‘show_in_rest’ => true And then I just had … Read more

How to echo data after a WP_Query

Within your Query loop, using your image URL as an example, put this: <?php $image_url = get_post_meta($post->ID, ‘image-url-field’, true); ?> Replace “image-url-field” with the name of your custom field. Then, also within that same loop, do this: <?php echo $image_url; ?> Reference: http://codex.wordpress.org/Function_Reference/get_post_meta

Sub-loop / nested loops Best Practices

First see When should you use WP_Query vs query_posts() vs get_posts()? My understanding of “The Loop” is that there is, and should only ever be, ONE “The Loop” per page/post – and that The Loop is responsible for iterating through that page/post/archive’s content. Yes, it’s commonly referred to as “main loop”. But “loop” in general … Read more

How to display specific posts with WP_Query?

Could be because you are passing an array of array try this: <?php $my_query = new WP_Query(); $my_query->query(array( ‘post__in’ => $curauth->user_favourite_post)); while ($my_query->have_posts()) : $my_query->the_post(); ?> <h3><a href=”https://wordpress.stackexchange.com/questions/27808/<?php the_permalink(); ?>”><?php the_title(); ?></a></h3> <?php endwhile; ?>