WP_Query exclude posts using meta query - php

Can't wrap my head around this one, would appreciate some help!
Overview
I have a "news" page with two seperate WP_Query loops. The first loop at the top of the page contains the 2 latest "featured" posts, defined by the following conditions;
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'posts_per_page' => 2,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => 'yes',
'compare' => 'LIKE',
),
),
);
The "featured_post" key relates to a custom ACF checkbox field. The client uses this to define featured posts in the backend of Wordpress.
Now in the second WP_Query loop, the remaining posts are displayed using the following conditions;
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => 'yes',
'compare' => 'NOT LIKE',
),
),
);
This essentially excludes the featured posts from this second loop.
Problem
My issue is the client doesn't always remember to untick the "featured" ACF field from the old featured posts when adding a new one. Therefore older featured posts that are no longer meant to be featured don't shift into the second loop.
Is there a way to use the meta_query in the second loop to exclude only the two latest featured posts - instead of all of them?
Or is there a better way?

I hope this will help you.
First, create a function on function.php like this:
<?php
function exclude_featured_post_IDs(){
$featured_post_IDs = array();
$args_featured = array(
'post_type' => 'post',
'orderby' => 'date',
'order' =>'DESC',
'posts_per_page' => 2,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => '"yes"',
'compare' => 'LIKE'
),
)
);
$query_featured = new WP_Query($args_featured);
if ($query_featured->have_posts()) :
while ($query_featured->have_posts()) :
$query_featured->the_post();
array_push($featured_post_IDs, get_the_ID());
?>
<?php
endwhile;
endif;
wp_reset_postdata();
return $featured_post_IDs;
} ?>
Then, Use this parameter 'post__not_in' => exclude_featured_post_IDs(), in the second WP_Query loop:
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'date',
**'post__not_in' => exclude_featured_post_IDs(),**
'posts_per_page' => -1,
);
?>

Related

Remove Featured Post from Wordpress Loop

I'm trying to figure out a way to exclude posts that are marked as "featured" from the "recent posts" page.
As you can see below, my recent posts page only shows posts from 2 categories (cat1 and cat2). The problem is, once in awhile, I'll add a post to one of the categories below, and mark it as "featured link" or whatever. So when you view the page, you basically see the featured post in the header(OUT of loop), and within the body (IN the loop). How would I go about removing the featured post that is IN the loop?
<?php
// args query
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'order' => 'DESC',
// display only posts in specifics categories (slug)
'category_name' => 'cat1, cat2'
);
// custom query
$recent_posts = new WP_Query($args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post();
?>
For anyone wondering, I tried the following, but it didn't work:
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'order' => 'DESC',
// display only posts in specifics categories (slug)
'category_name' => 'cat1, cat2',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'NOT LIKE',
),
),
);
When adding the 'meta_query' thing that others suggested, it seems the page stops displaying results all together.
Any idea of how to correctly write this out and maybe offer additional alternative solutions too?
Thanks.
Tested working good. just replace 'compare' => 'NOT LIKE' to 'compare' => 'NOT EXISTS'. Hope this help you.
// args
$args = array(
'numberposts' => 3,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
// query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
echo the_title();
endwhile;
endif;
wp_reset_query();
Another way is that you can use 'post__not_in' => array (//post ids you want to exclude) in your wp_query

How to query posts by ACF field but then also order by a separate ACF field?

I am using WP_Query to get posts that have a specific value in one of the ACF fields. I also need to order them by a separate ACF field. I am not sure how to accomplish this. Everything I've read says to use 'orderby' => 'meta_value' but I believe thats the value of the field I'm filtering the posts by, which is not what I want.
This is what I have right now..
$args = array(
'post_type' => 'contacts',
'posts_per_page' => -1,
'meta_key' => 'department',
'meta_value' => 'Transportation',
'orderby' => 'meta_value'
);
$the_query = new WP_Query( $args );
I need to orderby an ACF field named last_name.
It's possible to assign a name to a meta query, and then refer to that name in your orderby. Something like this.
$args = array (
'post_type' => 'contacts',
'post_status' => 'publish',
'nopaging' => true,
'posts_per_page' => -1,
'meta_query' => array( 'main_query' => array(
'key' => 'department',
'value' => 'Transportation'
), 'orderby_query' => array(
'key' => 'last_name',
)
),
'orderby' => array(
'orderby_query' => 'ASC',
),
);
$the_query = new WP_Query( $args );

Exclude Featured Posts in Wordpress 'Recent Posts' Function

Can I use the "exclude" array in the wp_get_recent_posts function to exclude Featured Posts? I have a plugin called NS Featured Posts which pulls featured posts through a key in the wp query i.e.:
$custom_query = new WP_Query(
array(
'post_type' => 'post',
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes'
)
);
could I somehow use this to target and exclude NS Featured Posts in the wp_get_recent_posts call e.g.:
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 3,
'exclude' => (the ns featured posts)
));
Thanks for any insight.
So I tested it now and getting posts without a certain meta key in one query is not possible.
But: You can exclude them from another query like so:
$featured_posts = get_posts( [
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'fields' => 'ids',
] );
query_posts( array( 'post__not_in' => $featured_posts ) );
while ( have_posts() ) : the_post();
$output .= '<li>'.get_the_title().'</li>';
endwhile;
wp_reset_query();
Functions such as wp_get_recent_posts() can accept all of the same arguments as WP_Query. While the documentation only lists a handful of parameters, the full set are available to you.
You had suggested using exclude in your query however that's going to want the IDs of the posts to exclude. You could of course grab those first but that's not going to be the most efficient solution.
The way to do this in a single query is with meta query options. The posts are being tagged with a meta key and a meta query will allow you to exclude those. You'll need to check both for the existence of the meta key and the value being 'yes'.
Example:
$recent_posts = wp_get_recent_posts( array(
'numberposts' => 3,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => '!=',
),
array(
'key' => '_is_ns_featured_post',
'compare' => 'NOT EXISTS',
),
)
) );
Reference: https://codex.wordpress.org/Class_Reference/WP_Meta_Query

WP_Query twice on one page - not getting the desired posts per page

I have a custom post type which has an ACF check box to define if an post is featured or not. I wanted to show 6 featured and 6 non featured posts on a page and so created 2 WP_Query loops both with separate args. Whilst the separation of featured and non-featured is working, the page only shows 6 posts in total and I'm not sure how to resolve that?
My code:
<?php
$args1 = array(
post_type => 'fairs',
posts_per_page => -1,
showposts => 6 ,
meta_key => 'start',
orderby => 'meta_value_num',
order => 'ASC'
);
$new1 = new WP_Query($args1);?>
<?php while ($new1->have_posts()) : $new1->the_post();?>
<?php $field = get_field( 'wa_feature', get_the_ID() ); if ( true == $field ):?>
Featured Posts
<?php endif;?>
<?php endwhile; wp_reset_postdata();?>
<?php
$args2 = array(
post_type => 'fairs',
posts_per_page => -1,
showposts => 6 ,
meta_key => 'start',
orderby => 'meta_value_num',
order => 'ASC'
);
$new2 = new WP_Query($args2);?>
<?php while ($new2->have_posts()) : $new2->the_post();?>
<?php $field = get_field( 'wa_feature', get_the_ID() ); if ( false == $field ):?>
Non-featured Posts
<?php endif;?>
<?php endwhile;?>
Your WP_Query isn't right. You do two completely identical queries ($args1 = $args2), and expects diffrent results from them. Also you put meta_key into query arguments, but without telling what and how to compare.
Depending on what type you have chosen, the right syntax, if your meta filed is named featured and the query can be:
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'meta_key' => 'featured',
'meta_value' => true //or 1, or 'yes` depending of ACF type you have choose
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'meta_key' => 'featured',
'meta_value' => false//or 0, or 'no`
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
or if this featured field exists only in featured posts then
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC'.
'meta_query' => array(
array(
'key' => 'featured',
'compare' => 'EXISTS',
),
)
);
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC'.
'meta_query' => array(
array(
'key' => 'featured',
'compare' => 'NOT EXISTS',
),
)
);
Check ACF query or Codex for the right sintax.
Your arguments should be quoted:
array(
'post_type' => 'fairs',
'posts_per_page' => 6,
'meta_key' => 'start',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
Array keys can either be integers, or strings. Read more in the PHP array() docs.
You should also only use posts_per_page in this case (and remove showposts, which was replaced by posts_per_page in WP 2.1).

Count all special custom fields per post of a category (Wordpress)

With this script my special custom field "Nickname" is counted only once, also if these field is available twice or more in a post.
Is there a way to count all special custom fields called "Nickname" in a post?
Thats the script:
<?php
$posts_with_nickname = get_posts(array(
'category__in' => array(2,5),
'meta_key' => 'Nickname',
'showposts' => -1,
));
printf('I have %d posts with "Nickname"!', count($posts_with_nickname));
?>
You may want to use meta_query
<?php
$posts_with_nickname = get_posts(
'category__in' => array(2,5),
'meta_query' => array(
array(
'key' => 'Nickname',
'value' => '',
'compare' => 'NOT LIKE',
),
),
'showposts' => -1,
);
printf('I have %d posts with "Nickname"!', count($posts_with_nickname));

Categories