Jump to content

[Solved] Preview of hidden/unpublished posts throws 404 error


rash
 Share

Recommended Posts

Hi guys,

I’m struggling with an annoying problem which I guess where it comes from, but can‘t find a way to get rid of it. When I try to preview hidden oder unpublished posts in the /kompendium/ category, I'll receive a 404 error, when I do the same thing with posts of other catgegories, everything works as expected. The only difference is an url thing I’ve bulit to shorten urls like /kompendium/entry to /entry/, while others are formed completely like /category/entry/.

To enable this, I use the following code in my home template:

<?php
if (strlen($input->urlSegment2)) {
    // 2 segments => 404
    throw new Wire404Exception();
}
else if (strlen($input->urlSegment1)) {
    // 1 segment, check whether it exists in /kompendium/
    $name = $sanitizer->pageName($input->urlSegment1);
    $post = $pages->get("/kompendium/")->child("name=$name");
    // if so render /kompendium/name/
    if($post->id) {
        echo $post->render();
      }
      // else 404
      else {
          throw new Wire404Exception();
      }
      $render_homepage = "no";
}

if ($render_homepage != "no") {

// render normal home content

}
?>

and in _init.php there is the following hook to ensure lists are generated in the wanted manner (all entries in /kompendium/ and only those work with template pg_entry)

<?php
ProcessWire\wire()->addHookBefore('Page::path', function($event) {
	$page = $event->object;
	if($page->template == 'pg_entry') {
		$event->replace = true;
		$event->return = "/$page->name/";
	}
});
?>

So everything is fine beside the preview/404 problem with hidden/unpublished posts. As some of them show quite complex content, the preview funtion is important. Any hints, why it doesn’t work?

Thanks
Ralf

 

Link to comment
Share on other sites

Hi @rash

Maybe you should include 

include=all

to your child selector like

if($user->isSuperuser()) {
	$post = $pages->get("/kompendium/")->child("name=$name, include=all");
} else {
	$post = $pages->get("/kompendium/")->child("name=$name");
}

 
  • Like 2
Link to comment
Share on other sites

Hi Zeka,

BINGO, that’s it, problem solved! The easyness and logic of your solution is probably a bit embarrasing for me, but sometimes I don’t see the most obvious things. So thanks a lot, you saved a lot of my time and nervs.

Ralf

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...