Jump to content
Sign in to follow this  
rash

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

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

 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...