Escape current post from loop

时间:2014-08-31 作者:Tarida George

我正在创建一个搜索小部件,为此我通过新的WP\\u Query()使用了一个二级循环;

$query = new WP_Query(\'s=searchTerm\');
if ($query->have_posts()){
    while ($query->have_posts()){
        $query->the_post();
        //echo the post
    }
    wp_reset_postdata();
}
else {
    echo \'No results\';
    die();
}
问题是,搜索查询返回标题或内容中包含搜索词的帖子+我正在查看的当前帖子/页面。我怎样才能避免呢?

2 个回复
最合适的回答,由SO网友:cybmeta 整理而成

问题的标题不准确。我认为您不想逃避第一篇帖子,而是想排除当前帖子。您可以使用post__not_in 中的参数WP_Query.

$args = array( \'s\' => \'searchTerm\' );

//Check if we are in a post of any type
if( is_singular() ) {
    $post = get_queried_object();
    $args[\'post__not_in\'] = array( $post->ID )
}

$query = new WP_Query( $args );
if ($query->have_posts()){
    while ($query->have_posts()){
        $query->the_post();
        //echo the post
    }
    wp_reset_postdata();
}

UPDATE

当您在ajax请求中使用代码时,您无法访问ajax操作挂钩中的当前post数据。您需要在ajax请求数据中传递当前的post ID。

例如(未测试。仅在此处书写):

//Enqueue the scripts and localize js variables
add_action(\'wp_enqueue_scripts\', \'cyb_scripts\');
function cyb_scripts() {

    //register my-script
    wp_register_script( \'my-script\', \'/url/to/my-sript.js\', array(\'jquery\') );

    //enqueue my-sript and dependencies
    wp_enqueue_script(\'jquery\');
    wp_enqueue_script(\'my-script\');

    //Localize script data to use in my-script. Set here the post ID to exlude
    $exclude_post = 0;
    if( is_singurlar() ) {
        $current_post = get_queried_object();
        $exclude_post = $current_post->ID;
    }
    $scriptData = array(
                      \'ajaxurl\' => admin_url(\'admin-ajax.php\');
                      \'exclude_post\' => $exclude_post
                     );
     wp_localize_script(\'my-script\', \'my_script_data\', $scriptData);

}

//Ajax action hooks
add_action(\'wp_ajax_nopriv_process_ajax\', \'cyb_process_ajax\');
add_action(\'wp_ajax_process_ajax\', \'cyb_process_ajax\');
function cyb_process_ajax(){

    $args = array( \'s\' => \'searchTerm\' );

    //Check for post to exlude
    if( isset($_GET[\'exclude_post\']) ) {
        $args[\'post__not_in\'] = array( intval( $_GET[\'exclude_post\'] ) );
    }

     $query = new WP_Query( $args );
     //....
     wp_reset_postdata();

}
javascript:

jQuery(document).ready(function($){

    $.ajax({

        type: "GET",
        url: my_script_data.ajaxurl,
        data: {
            action: "process_ajax",
            exclude_post: my_script_data.exclude_post
        }
    })
    .done( function( response ) {
        //....
    })
    .fail( function( response ){
        //....
    });
});

SO网友:Abhik

尝试添加offset 参数转换为参数。。

$query = new WP_Query( array( \'offset\' => 1, \'s\' => \'searchTerm\') );
    if ($query->have_posts()){
        while ($query->have_posts()){
            $query->the_post();
            //echo the post
        }
        wp_reset_postdata();
    }
    else {
        echo \'No results\';
        die();
    }

结束

相关推荐

Featured posts and the loop

我正在尝试在索引中添加一个框。php在何处显示最新的3篇特色帖子。根据this tutorial, 我已将以下内容添加到functions.php:add_theme_support( \'featured-content\', array( \'filter\' => \'magdeleine_get_featured_posts\', \'max_posts\' => 3, ) ); function magdeleine