The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Shwing posts by post_parent (4 posts)

  1. Cailean
    Posted 10 years ago #

    I'm writing a plugin that assigns posts to particular pages (see genesis here http://wordpress.org/support/topic/46002).

    In my page.php template, I need two loops... The first shows the content of the page itself (the default behaviour) the second should show all posts for which post_parent is set to the current page_id...

    I've gotten a filter to achieve the second loop, but it borks the first one...

    Is get_posts the answer? query_posts()?

  2. Cailean
    Posted 10 years ago #

    Here's the filter I'm using...

    function p2p_query($where) {
    global $post_id, $wpdb;
    $where = " AND post_parent=$post_id";
    return $where;
    add_filter('posts_where', 'p2p_query');

  3. Cailean
    Posted 10 years ago #

    okay, ignore the code snippet above... I see the error of my ways!

    I am 99% there I just have one little issue - the most recent post is not being shown!

    Here is all the relevant plugin code:

    function p2p_parentSelect() {
    global $post_ID, $wpdb;
    $post_parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = $post_ID ");

    echo '<fieldset id="postparent" rel="'.$post_parent.' - '.$post_ID.'">';
    echo '<legend>Assign to Page</legend> ';
    echo '<div><select name="parent_id">';
    echo '</select></div></fieldset>';
    add_action('simple_edit_form', 'p2p_parentSelect');
    add_action('edit_form_advanced', 'p2p_parentSelect');

    function p2p_query($where) {
    $where = " AND post_parent=".$_GET['page_id'];

    return $where;
    function p2p_func(){
    add_filter('posts_where', 'p2p_query', 11);

    the first loop in the page runs normally and then p2p_func() is called immediately before the second loop... works except most recent post is always missing!

  4. Cailean
    Posted 10 years ago #

    any help here, folks?

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.