1

We can get multiple posts manually in a page, but that's generated in a template, AFTER the default query returned something else (page/post, etc)

How do you query multiple posts in a public query, which share no taxonomy or anything else?

e.g. by ID:

http://example.com/?p=23,18,2,199,6,8

I got it working already parsing a variable in a page-name.php template, but once it gets the template it's too late to generate e.g. canonicals.

I guess this would be something similar to a search, except the search is a 1-to-multiple-posts relation and this requires a multiple-queries-to-unique-post (each).

Thanks.

2 Answers 2

5

Maybe you're looking for the post__in parameter in WP_Query.

$query = new WP_Query(array(
    'post__in' => array(23,18,2,199,6,8)
);

And then:

while ( $query->have_posts() ) {
    $query->the_post();
    /* post loop */
}

Take a look at the docs. =D

For public queries:

post__in is not public queryable by default, so you can just validate and copy $_GET['post__in'] on the parse_query action hook, and let the thing happen.

add_action('parse_query', 'wpse59828_parse_query');
function wpse59828_parse_query($query) {
    if (empty($_GET['post__in']))
        return $query;

    $posts = explode(',', $_GET['post__in']);
    $post__in = array();
    foreach ($posts as $p) {
        $post__in[] = intval($p);
    }

    $query->query_vars['post__in'] = $post__in;
    return $query;
}

Then you would just access this:

http://mywebsite.com/?post__in=23,18,2,199,6,8

Please note that, like this, you won't be able to set the post order in WordPress versions before 3.5 (#13729). Use this plugin if you need to.

4
  • Yes, but I'm trying to get it to work through the PUBLIC query, like mywebsite.com/p=23,18,2,199,6,8, or mywebsite.com/p=post__in=23,18,2,199,6,8, or mywebsite.com/post_parent=12 (this last one would be for pages, and yes, they have something in common, but this one is also defined as "public" and doesn't work either)
    – sergio
    Oct 25, 2012 at 5:38
  • @sergio, check the edit. Oct 25, 2012 at 14:02
  • Will parse_query hook happen soon enough for the rest of the default functions work accordingly to the queried pages? e.g. everything should match the current query, like multiple posts (archive?) template used, sidebar content and body classes properly for the multiple query? I'm affraid some of the content to be related to something like a "page" while others to the e.g. last post in this query
    – sergio
    Jan 3, 2013 at 4:17
  • Hi @sergio. parse_query will run before fetching stuff from database and sending you to the template, so you'll probably have to build your own solution for most of these questions. You can intersect the global $wp_query in this very same function, and then set the template, body class, sidebar and so on... Jan 4, 2013 at 11:35
0
$tax_query = array (
    array (
        'taxonomy' => 'product_cat',
        'field' => 'slug',
        'terms' => $cat_name
        )
    );

$args = array( 'tax_query' => $tax_query, 'numberposts' => -1);
$lastposts = get_posts( $args );
1
  • 2
    It's cool that you have some code that might slove the OP's problem, but adding a description of exactly what the code does and how it works would be even better
    – shea
    May 8, 2013 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.