Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

wp_query include 1 post from another post-type WordPress

  • SOLVED

Hello

I have this query...

<?php

$banner = new WP_Query(array(

'post_type' => 'features',
'orderby' => 'rand',
'posts_per_page' => -1

)); ?>



and I am trying to include one post with the id 382. But this post is in another custom post type called 'zones'

I tried this but it just breaks the query...

<?php

$banner = new WP_Query(array(

'post_type' => 'features',
'orderby' => 'rand',
'posts_per_page' => -1,
'post__in' => 382

)); ?>


Can anyone help me include '382' into this query?

Thanks

Working answer gets the full amount.

Answers (2)

2012-08-19

Pavel Petrov answers:

Is there only one post in zones? If yes try:

<?php
$banner = new WP_Query(array(
'post_type' => array('features', 'zones')
'orderby' => 'rand',
'posts_per_page' => -1,
)); ?>

You can also try(that should work):

<?php
$ids = $wpdb->get_col("SELECT `ID` FROM `wp_posts` WHERE `post_type` = 'features'");
array_push($ids, "318");
$banner = new WP_Query(array(
'post_type' => array('features', 'zones'),
'post__in' => $ids,
'orderby' => 'rand',
'posts_per_page' => -1,
));

?>


Josh Cranwell comments:

Nope - there is a few in zones...

Ahh maybe I should I just us post__in an array and scrap the post type all together...


Josh Cranwell comments:

That is a really nice way of doing it - but it does not work...


Josh Cranwell comments:

But I just tried the basic post__in

<?php

$banner = new WP_Query(array(

'orderby' => 'rand',
'posts_per_page' => -1,
'post__in' => array(382, 340, 338, 336, 334, 332, 330, 328)

)); ?>

And this don't work.... hmmm very odd


Josh Cranwell comments:

actually this works...


$banner = new WP_Query(array(

'post_type' => array('features', 'zones'),
'orderby' => 'rand',
'posts_per_page' => -1,
'post__in' => array(382, 340, 338, 336, 334, 332, 330, 328)

// Eurosport, KTM, ACU, Test Ride, Yamaha Off Road, First Licence, Get On, Ramp'd up

));




But yours looks like a nicer way of doing it, but it returns nothing :/


Pavel Petrov comments:

Can you try doing print_r after $ids = $wpdb->get_col("SELECT `ID` FROM `wp_posts` WHERE `post_type` = 'features'"); and give me the output:
should look something like this:

$ids = $wpdb->get_col("SELECT `ID` FROM `wp_posts` WHERE `post_type` = 'features'");
print_r($ids);


Pavel Petrov comments:

Also try rearranging my query:


<?php

$ids = $wpdb->get_col("SELECT `ID` FROM `wp_posts` WHERE `post_type` = 'features'");
array_push($ids, "318");
$banner = new WP_Query(array(
'post_type' => array('features', 'zones'),
'orderby' => 'rand',
'posts_per_page' => -1,
'post__in' => $ids

));



?>


Josh Cranwell comments:

Array ( )

Thats all it say. Dont worry for now I've got the result I need.

Thanks for your help.


Josh Cranwell comments:

Nope - not working

2012-08-19

ej_emman answers:

Hello Josh,

I think, the best Idea is to include features and zones post type in your argument.
Then filter them into the loop.


Josh Cranwell comments:

Thanks ej_emman but I found a solution in the end. I have voted all the money to Pavel earlier to close the question.