Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

php - post__not_in is not excluding IDs from Wordpress Search Query

I am trying to retrieve the IDs of posts with a certain meta value (this works fine) I then try to pass them through post__not_in and it does not exclude the posts from the wordpress search.

I have the integer array (from a var_dump):

array(2) { [0]=> int(373) [1]=> int(247) }

However, I now need to convert that array into 373,247 for use in post__not_in. Any ideas?

remove_action('pre_get_posts','exclude_pages_from_search');

$hidePages = new WP_Query( array (
    'post_type' => array( 'post', 'page', 'offer', 'review', 'project' ),
    'ignore_sticky_posts' => true,
    'posts_per_page' => -1,
    'meta_key' => 'edit_screen_sitemap',
    'meta_value' => 'hide',
    'fields' => 'ids'
));

$test = $hidePages->posts;

function exclude_pages_from_search($query) {
    if ( !is_admin() ) {

        if ( $query->is_main_query() ) {

            if ($query->is_search) {
                $query->set('post__not_in', $test);
            }
        }
    }
} add_action('pre_get_posts','exclude_pages_from_search');
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue I was orginally having was I was imploding the array and turning it into a string when I needed an integer array. So I removed the imploding of the Query and left it as a variable which of course became an integer array as post___not_in only allows integer arrays.

I then ran into the trouble of a memory leak if I ran the query inside the function so I had to figure out how to run it outside the function and still be able to use the variable inside the function as it would be undefined.

With the addition of global $hidePageIds, I was able to access the variable holding the integer array inside the function and therefore pass it in the post__not_in query.

remove_action('pre_get_posts','exclude_pages_from_search');

$hidePages = new WP_Query( array (
    'post_type' => array( 'post', 'page', 'offer', 'review', 'project' ),
    'ignore_sticky_posts' => true,
    'posts_per_page' => -1,
    'meta_key' => 'edit_screen_sitemap',
    'meta_value' => 'hide',
    'fields' => 'ids'
));

$hidePageIds = $hidePages->posts;

function exclude_pages_from_search($query) {
    if ( !is_admin() ) {

        if ( $query->is_main_query() ) {

            if ($query->is_search) {

                global $hidePageIds;
                $query->set('post__not_in', $hidePageIds);
            }
        }
    }
} add_action('pre_get_posts','exclude_pages_from_search');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...