0

I just stumbled over an issue while programming my own WordPress theme and I need your help.

I defined an author.php file which should display the static pages and the blog pages of a given author. You have to know, that our website has a lot of pages with sub-, subsub- and even subsubsub- pages. All these pages were publishes by the "webmaster"-Account. Inside these pages, our content pages are situated. These child pages are published by our different authors and should be displayed on the author-page.

Example:

Practical courses (Webmaster)

  • Chemistry (Webmaster)

    • Qualitative analysis (Webmaster)

      • Proofs for anions (Webmaster)

        • Sulfide (Author)

        • Carbonate (Author)

        • ... (Author)

      • Proofs for cations (Webmaster)

        • ...
    • Physical chemistry (Webmaster)

      • ...
    • Physics (Webmaster)

      • Light diffraction (Webmaster)

In this example the "Sulfide" page would be the actual content. And that page should be visible on the author page of the author that published id.

I now added this code to display the pages of a specific author:

$author_id = $_GET['author'];

$args = array(
  'sort_order' => 'asc',
  'sort_column' => 'post_title',
  'authors' => $author_id,
  'post_type' => 'page',
  'post_status' => 'publish'
  );

$pages = get_pages( $args );

   foreach ( $pages as $page ) { ?>
      <div class="col-md-4 article-item" style="cursor: pointer;" onclick="location.href='<?php echo get_page_link( $page->ID ); ?>'">
         <h4><?php echo $page->post_title; ?></h4>
         <?php if ( has_post_thumbnail($page->ID) ) {
            echo get_the_post_thumbnail( $page->ID, 'post-thumbnail', array( 'class' => 'col-img' ) );
         }
         else {
            echo '<img src="https://placehold.it/200x200?text=Icon" class="col-img wp-post-image round" />';
         }
         ?>
         <p class="excerpt"><?php echo $page->post_excerpt; ?></p>
      </div>
   <?php } ?>

Unfortunately every page is returned for every author and not only the pages published by that specific author.

If I hardcode the ID to a specific value - of an author that has published some content pages on our side - ($author_id = 2;) nothing is returned. But if hardcoding the ID of the webmaster all the posts of the webmaster are shown (without the content posts which were published by an author)! So it seems to rely on the fact, that the content pages are subpages. For me, WordPress first checks if the parent pages are published by the given ID. If there are some, it checks if there are subpages of these parent pages which are published by that same ID.

In my case, it checks if the author of the ID "2" has published a parent page and gets a boolean of "false" and so does not even check to find subpages...

Do you know how I could edit the query so that the child pages are returned even if the parent page was published by an other author?

Thanks in advance!

5
  • 1
    Returning all posts tends to be a behavior when parameters don't have the values you think they should have. I'd recommend checking that the $author_id is being populated properly on the page. Instead of setting it from the the $_GET, hardcode it to a particular author's ID and see if the query works that way.
    – Joel
    Commented Apr 30, 2016 at 14:36
  • @Joel: Thanks for your idea. Unfortunately this does not solve the problem. If I hardcode the id to a specific value no posts are shown at all. I'll add some information to the post above, so please check it in a few minutes.
    – Sam
    Commented Apr 30, 2016 at 15:37
  • What IS the value of $author_id? The codex says that the 'authors' parameter takes a string. codex.wordpress.org/Function_Reference/get_pages
    – frogg3862
    Commented Apr 30, 2016 at 17:18
  • Good point from @frogg3862. Try strval($author_id) in your query to convert it from an integer to a string. The fact that putting the ID in there causes nothing to be returned would seem to indicate that the problem is with that parameter.
    – Joel
    Commented Apr 30, 2016 at 17:56
  • I included the strval($author_id) now, but unfortunately it only "reversed" the problem. Now the authors page shows every page, even those that were not published by that author But I think that I see what Wordpress does here: It seems, that every page is output which contains a page posted by that author. Ironically this is the exact opposite of the previous behavior...
    – Sam
    Commented Apr 30, 2016 at 18:28

1 Answer 1

1

Solved


I've solved the problem and I'm going to share the code I used, so other developers that have this issue too can cope with it.

The get_pages()-function accepts the "hierarchical" argument. By default it's set to 1 (true), which means, that Wordpress searches the pages in hierarchical order to find the other parameters. If - as in my case - you search for pages of an author that are children of pages posted by a different author, Wordpress will not find them! To avoid this undesired behavior, you have to set the "hierarchical" argument to false (= 0).

This tells Wordpress to search the pages for the given arguments without adhering to the hierarchy.

So to return all the pages of a given author, you have to use the following code:

$pages_args = array(
  'sort_order' => 'asc',
  'sort_column' => 'post_title',
  'authors' => $author_id, // has to be a string!!!
  'post_type' => 'page',
  'post_status' => 'publish',
  'hierarchical' => 0
);

Your Answer

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

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