WordPress.org

Forums

[resolved] several post_parent for wp_query? (13 posts)

  1. shin
    Member
    Posted 3 years ago #

    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!

  2. David Gard
    Member
    Posted 3 years ago #

    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?

  3. shin
    Member
    Posted 3 years ago #

    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)

  4. shin
    Member
    Posted 3 years ago #

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

  5. David Gard
    Member
    Posted 3 years ago #

    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.

  6. shin
    Member
    Posted 3 years ago #

    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.

  7. David Gard
    Member
    Posted 3 years ago #

    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;
    }
  8. shin
    Member
    Posted 3 years ago #

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

    but thanks, i will know this hook now.

  9. David Gard
    Member
    Posted 3 years ago #

    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;
  10. shin
    Member
    Posted 3 years ago #

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

  11. David Gard
    Member
    Posted 3 years ago #

    You are welcome, glad you got it sorted.

  12. Chris Demetriad
    Member
    Posted 3 years ago #

    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.

  13. David Gard
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic