wordpress/php-fpm/wordpress_files/themes/sustrai-twentytwelve/wip.txt

141 lines
4.3 KiB
Plaintext
Raw Normal View History

2020-05-22 03:40:23 +02:00
https://wordpress.stackexchange.com/questions/72099/can-i-exclude-a-post-by-meta-key-using-pre-get-posts-function
I want to post my temporary solution for featured posts in case some people may make use of it. I don't use pre_get_posts hook here but not query_posts either. The problem is that I have to play with the main query and have to run a piece of sql query. I would be happy if any experts could check the code and let me know whether it's OK and will not cause any performance issues. It will also be great if anyone has a better approach and share it with us.
Create featured posts query
<?php
$featured_query = new WP_query( array(
'meta_key' =>'featured',
'meta_value' =>'yes',
'posts_per_page' => 5,
'no_found_rows' => true
)
);
while ($featured_query->have_posts()) :
$featured_query->the_post();
//Stuff...
endwhile;
wp_reset_postdata();
?>
Create the main query, exclude the posts which has the featured meta_key, limit the exclusion to 5 most recent posts and show all others.
<?php
$excludeposts = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'featured' AND meta_value != '' ORDER BY post_id DESC LIMIT 0, 5" );
$main_query = new WP_Query( array(
'post__not_in' => $excludeposts,
'paged' => $paged
)
);
while ($main_query->have_posts()) :
$main_query->the_post();
//Stuff...
endwhile;
?>
shareimprove this answer
edited Oct 1 '15 at 16:26
denis.stoyanov
1,1061714
answered Nov 10 '12 at 16:27
Carlisle
81115
add a comment
up vote
0
down vote
In response @Carlisle, if you want exclude the most 5 recent posts marked featured, you could do the following. Change the posts_per_page to how many you want to exclude, and the meta_query to how you are designating the featured category.
function cmp_exclude_featured_posts($query) {
$exclude = array(); //Create empty array for post ids to exclude
if ( $query->is_main_query() ) {
$featured = get_posts(array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'featured',
'value' => '1',
'compare' => '==',
),
),
'posts_per_page' => 2
));
foreach($featured as $hide) {
$exclude[] = $hide->ID;
}
$query->set('post__not_in', $exclude);
}
}
add_filter( 'pre_get_posts', 'cmp_exclude_featured_posts' );
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
-- https://wordpress.stackexchange.com/questions/71576/combining-queries-with-different-arguments-per-post-type
------------------------------------------------------------------------------------------------------------
-
wn vote
@mridual aggarwal your answer is very very good but unfortuntly it is not really combining the 2 wp_query it is only shows the posts from both in arrange i mean 5 posts from the first & 5 from the second but not sorted all in one so i have this solution & it exactly achieved the goal for my self at least
<?php
$term = get_term_by( 'slug', get_query_var( 'tag' ), "post_tag" );
$tagslug = $term->slug;
$post_types = get_post_types('','names');
?>
<?php
//first query
$blogposts = get_posts(array(
'tag' => $tagslug, //first taxonomy
'post_type' => $post_types,
'post_status' => 'publish',
));
//second query
$authorposts = get_posts(array(
'bookauthor' => $tagslug, //second taxonomy
'post_type' => $post_types,
'post_status' => 'publish',
));
$mergedposts = array_merge( $blogposts, $authorposts ); //combine queries
$postids = array();
foreach( $mergedposts as $item ) {
$postids[]=$item->ID; //create a new query only of the post ids
}
$uniqueposts = array_unique($postids); //remove duplicate post ids
$posts = get_posts(array(
//new query of only the unique post ids on the merged queries from above
'post__in' => $uniqueposts,
'post_type' => $post_types,
'post_status' => 'publish',
));
foreach( $posts as $post ) :
setup_postdata($post);
?>
// posts layout
<?php endforeach; ?>
<?php wp_reset_postdata();?>