Jump to content

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


Neo
 Share

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.

Link to comment
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
Link to comment
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
Link to comment
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
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...