Jump to content
Sign in to follow this  
Neo

How to add a unique value to a link and delete all children of a page programmatically ?

Recommended Posts

Hey,

I have a small niche job board that is based on ProcessWire 3.0.62 and encounter 2 problems:

1. Job pages are unpublished after a certain amount of time using a cron job and then moved as children to the "Expired Jobs" page.

Now, it is a bit annoying having to delete those old, unpublished job pages individually.

What is the best way to programmatically delete all children of the "Expired Jobs" page in one go using a protected template in the backend?

 

2. I am starting to get a problem with many similar job titles. Although, Processwire will add a number to the end of a link, when a certain job title already exists (e.g. My-Job-1, My-Job-2), this causes problems from time to time with the unpublishing process, for instance

Can't save page 1997: /expired-jobs/product-designer-2/: Chosen parent '/expired-jobs/' already has a page named 'product-designer-2'

What would be the best way to add a unique identifier to each link (e.g. combination of 6 or more random numbers and letters) to ensure unique links despite equal job titles?

 

Thanks for your feedback.

Share this post


Link to post
Share on other sites

You could do the following in your template to delete pages:

$jobs = $pages->find("template=your-template, parent.name=expired-jobs");
foreach($jobs as $job) {
	//$job->trash(); this will trash the page
	$job->delete();
  }

How are you currently creating the jobs? Are you creating them from a front end form and using the api to create the new page? 

  • Like 2

Share this post


Link to post
Share on other sites

 

36 minutes ago, Neo said:

What would be the best way to add a unique identifier to each link (e.g. combination of 6 or more random numbers and letters) to ensure unique links despite equal job titles?

If it's worth making the update, there is a new class PageNames with methods for this purpose.

https://processwire.com/blog/posts/pw-3.0.111/

  • Like 2

Share this post


Link to post
Share on other sites

Thanks for your feedback.

Job pages are created using a front end form.

In order to trash the old jobs, I ended up with the following code:

$jobs = wire('pages')->find("template=job-template, parent=/expired-jobs/, include=unpublished");

foreach($jobs as $job) {
	$job->trash();
}

Now, regarding the problem of adding a unique value to each job link, I guess I would have to update Processwire first in order to access the PageNames and WireRandom classes?

 

  • Like 1

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...