• Resolved shin

    (@sorashin)


    i want to show 15 random thumbnails from three parent pages. there is the code:

    function homepics () {
    	$the_query = new WP_Query ('post_type=page&post_parent=155+165+104&orderby=rand&showposts=15');
    	while ( $the_query->have_posts() ) : $the_query->the_post();
    		echo '<li>';
    			echo '<a href="'.get_permalink().'">';
    			the_post_thumbnail ('thumbnail');
    			echo '</a>';
    		echo '</li>';
    	endwhile;
    	wp_reset_query ();
    }
    ?>

    with post_parent=155+165+104 or 155,165,104 wp don’t work correctly, it’s taking all pages and give me some empty li (from pages not contain thumbnail).

    help me plz!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The WP_Query class only accepts an integer for the ‘post_parent’ argument, so you need to use the ‘posts_where’ filter.

    add_filter('posts_where', 'add_post_parents', 1);
    function add_post_parents($where){
        {Do what ever you need to to the string to add your extra post parents}
    
        return $where;
    }

    P.s. It looks like you have an unclosed tag in your post that is causing the page to display in correctly – could you edit to correct this please?

    Thread Starter shin

    (@sorashin)

    thanks!

    i’am so sorry but i don’t understand how i can apply this code for my function( i really new with php.

    i must write ‘post_parent=$where’ or what? and where i must specify my three pageID?

    sorry, i’am just from russia)

    Thread Starter shin

    (@sorashin)

    yeah, sorry, it was unclosed li, fixed now.

    Thanks for sorting. And I’d suggest googling ‘wordpress filter’ for examples of how to use filters.

    Basically $where will be the WHERE portion of the SQL query that wordpress is going to send to your database. You need to alter $where so that instaed of just having one post parent it in it has all of the ones that you want.

    Thread Starter shin

    (@sorashin)

    i read but can’t anderstand completely. i must write in function cmth like that:

    add_filter('posts_where', 'add_post_parents', 1);
    function add_post_parents($where){
        $where .= "post_parent = '155' AND post_parent = '165'";
        return $where;
    }

    before my function. but what next i don’t understand( must i write in function:
    $the_query = new WP_Query ($where .'&post_type=page&post_parent=104&orderby=rand');
    ?

    just correct me to right side plz. thanks.

    The part where you run the query stays the same, except that you take the ‘post_parent’ argument out altogether –

    $the_query = new WP_Query('post_type=page&orderby=rand&showposts=15');

    Then, before the databae is queried, we add the criteria for post_parent using the ‘posts_where’ filter (put the below code at the top of the template that you are using) –

    add_filter('posts_where', 'add_post_parents', 1);
    function add_post_parents($where){
        $where .= " AND (post_parent = '104' OR post_parent = '155' OR post_parent = '165')";
        return $where;
    }
    Thread Starter shin

    (@sorashin)

    wow! it’s work for homepage, but now my templates don’t work correctly(

    but thanks, i will know this hook now.

    The trink is to add the filter only when you want to use it. Have a look at the is_page() function, that will probably help.

    if(is_page(99)) : // Where 99 is the page that you want to use this filter.
        add_filter('posts_where', 'add_post_parents', 1);
    endif;
    Thread Starter shin

    (@sorashin)

    you’re my hero, duck_boy, you know?
    with is_home() all works fine. i’am saved.
    sincerely THANK you, friend!

    You are welcome, glad you got it sorted.

    duck__boy, I’d like to thank you too for this solution.

    It has helped me understand these filters better and obviously saved my ass big time.

    Again, you are welcome. Good to see that the forum works! 🙂

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘several post_parent for wp_query?’ is closed to new replies.