Wordpress Câu query loại bỏ post hiện tại khỏi bài viết liên quan

Written by Tommy
Posted on Mon, 11/08/2021 - 15:34
9 views

Share Everywhere

Table of contents

If you want to build a simple, similar post list – in a single page view – when you query the latest or random posts from a post type you can use WP_Query.

The only thing you should watch is the current post which you should exclude from the query. To do this, you must get the current post’s id with the help of the get_the_ID() function and add it as a value to the post__not_in parameter like so:

Câu query đây

$args = array (
    'post_type'              => array( 'jobs' ),
    'nopaging'               => false,
    'posts_per_page'         => '5',
    'ignore_sticky_posts'    => false,
    'post__not_in'           => array(get_the_ID())
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) : 
    while ( $query->have_posts() ) : $query->the_post();
        // HTML template
    endwhile;
endif; 

wp_reset_postdata();

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
2 + 16 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Profile picture for user Tommy
Câu nói tâm đắc: “Điều tuyệt với nhất trong cuộc sống là làm được những việc mà người khác tin là không thể!”