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
Related
I am trying to filter custom post types on my WordPress site (using BeTheme) based on a Checkbox field. I've managed to get the field of the posts and display the correct posts when the field is a Select field, but I've read that the code needs to be changed for check boxes because they use serialization? I'm relatively new to PHP's deeper parts, so I think I'm confusing some concepts.
This is my code in includes/content-post.php:
global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'meta_key' => 'region', 'meta_value' => 'toronto', ) );
query_posts( $args );
and this is the code I use in my archive page template:
$acf_checks = get_field('region');
$meta_query[] = array(
'key' => 'region',
'value' => serialize($acf_checks),
'compare' => 'LIKE',
);
$args = (array (
'post_type' => 'indiRes',
'posts_per_page' => -1,
'meta_query' => $meta_query
//'meta_key' => 'region',
//'meta_value' => 'toronto',
// 'orderby' => 'date',
// 'order' => 'DESC'
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<p>
<?php the_title(); ?>
</p>
<p class="1">
<?php the_field('region', get_the_ID()); ?>
</p>
<?php endwhile; ?>
I've tried a few different variations of code by references pages like this and this but I'm sorta getting lost in what needs to be referenced and how.
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,
);
?>
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
So, I am trying to show items from a specific category in the woocommerce:
Here is what I have so far:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => '', 'orderby' => 'date', 'order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
<div class="content"> Content </div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
When the product_cat is empty, it shows all the items. I want to include "exclude category".
For example, I want to show all but items in a "no_good" category.
Could someone help me out with it?
Also, how can I add a pagination to this?
Thanks!
Is product_cat your custom taxonomy? If it is then you need to modify your $args with tax query:
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'tax' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => array('no_good'),
'operator' => 'NOT IN',
),
),
);
...
This assumes that "no_good" is a product_cat name. Adjust field if it isn't.
Regarding the pagination part, do check the codex article regarding pagination.
Well I am stuck in a sorting posts in wordpress! I know this is a previously asked question, but no luck for me the codes and solutions I found here!
The below is code snippet I am using.
$args = array(
'showposts'=>10,
's' => $search,
'meta_key' => 'neighboorhood',
'meta_value' => $location,
'orderby' => 'meta_value_num',
'meta_key' => 'is_sort',
);
That can not work, since you use meta_key twice in your array. You should use an meta_query for the where clause, and meta_key only for the sort.
$args = array(
'showposts'=>10,
's' => $search,
'meta_query' => array(
array(
'key' => 'neighboorhood',
'value' => $location
)
),
'meta_key' => 'is_sort',
'orderby' => 'meta_value_num'
);
Just use below mentioned code to fetch posts order by meta_value :
<?php
$myargs = array(
'posts_per_page' => 4, //number of post to show
's' => $search,
'meta_key' => 'is_sort', //name of meta field
'orderby' => 'meta_value_num',
'order' => 'ASC', // you can modify it as per your use
'meta_query' => array(
array(
'key' => 'neighboorhood',
'value' => $location,
)
)
);
query_posts($myargs );
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title();?></h2>
<p><?php the_content();?></p>
<?php endwhile;
endif;
wp_reset_query(); ?>
Just use the above mentioned code, you will get your desired result. If you are using this process in case of date field, then please check here for better understanding : http://www.wptricks24.com/how-to-order-wordpress-post-by-custom-field-date