A
A
Alex2018-07-16 17:47:03
WordPress
Alex, 2018-07-16 17:47:03

How to display in a WordPress table only certain posts by their ID?

I am writing a plug-in for the VI that performs some kind of manipulation with records. After that, I want to redirect the user to a table with records, but at the same time, by default, only display those that I have changed. I tried adding a parameter to the link post__inlike WP_Query- /wp-admin/edit.php?post__in=1,2,3,4- but that didn't work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Yanchevsky, 2018-07-16
@Kozack

I haven't tested the code, but I think it's worth digging somewhere in this direction.

function posts_for_specific_ids($query) {
    global $pagenow;
 
    if( 'edit.php' != $pagenow || !$query->is_admin )
        return $query;

    $ids = explode(',', $_GET['post__in']);
    $query->set('post__in', $ids);

    return $query;
}
add_filter('pre_get_posts', 'posts_for_specific_ids');

What are snippets and how to use them in WordPress

A
Ainur Valiev, 2018-07-16
@vaajnur

why are you trying to get posts with a get request? do as stated in the documentation

$args = array(
    'post__in' => array(43,23,65)
);

$posts = get_posts($args);

foreach ($posts as $p) :
    //post!
endforeach;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question