List of URLs from multi-value field

(I’m an absolute beginner at PHP, I worked in other coding languages.)

This pertains to a Wordpress website.

Please point to appropriate forum if this is not the right one.

I have a text field with words such as “Big dog”, “Small cat”, “École”, “Mac & Cheese”, etc. Number of values in the field will vary, currently comma separated. The field type is currently set to text, that can be changed.

I need to do the following in PHP.

(1) I need to first sort the list into alpha order, as there’s no guarantee it will have been created in alpha order.
(2) The words are titles of Wordpress pages. Given those titles, I need to look up the slug for each title. If no slug is found, then I need the operations to fail silently on that lookup and skip by it rather than blowing up.
(3) Then, I need generate a list of URLs from the results:

asdf

(Skipping the generation of a URL for any slugs that were not found.)

Help in writing this, or pointing to a better place for this, is appreciated. Many thanks.

Where do you look up the ‘slugs’? (You have provided any of that information?)

I’m assuming its a database/table of some kind? (but now name or layout/structure provided)…

Kinda hard to help on that part… no?

I have an example worked up (not completed)… still working on parameterizing the IN clause array stuff…

need o run some errands… I’ll post an example code later (unless you already have this already figured out)

Hi Whispers, I don’t know how to look up the slugs. In my thinking, I would take the title entered into the multi-value field (say “Big dog”), and then using that as a lookup key, key on that to look up the slug in the Wordpress database. I don’t know if that is even possible.

Well anything is possible :slight_smile: … you just need the right information to succeed I guess.

I dont mess with wordpress much (if ever)… and def havent touched anything wordpress related in years… so I cant guide on what tables this needs to point at… but this code would do what you ask IMHO…

include('../test_db_pdo.php');

$targetString = 'Big dog,Small cat,Ecole,Mac & Cheese';
$individualSlugNameArray =  explode(",", $targetString);
sort($individualSlugNameArray);

$getAvailbleSlugs_sql = 'SELECT * FROM `php_help` WHERE slugname IN(';
$placeHolderString = '';
for($i=0; $i<count($individualSlugNameArray); $i++){
	$getAvailbleSlugs_sql .= $placeHolderString.':val'.$i;       // :val0, :val1, ...
	$placeHolderString = ',';
}
$getAvailbleSlugs_sql .= ');';
$getAvailbleSlugs_stmt = $conn->prepare($getAvailbleSlugs_sql);

for($i=0; $i<count($individualSlugNameArray); $i++){
  $getAvailbleSlugs_stmt->bindValue(':val'.$i, $individualSlugNameArray[$i]);
} 

$getAvailbleSlugs_stmt->execute();
$getAvailbleSlugs_stmt->setFetchMode(PDO::FETCH_ASSOC);
$slugList = $getAvailbleSlugs_stmt->fetchAll();
$total_slugs = $getAvailbleSlugs_stmt->rowCount();
		
if($total_slugs <= 0){
	echo 'no results found';	
}else{
	for($i=0; $i<$total_slugs; $i++){
		echo '<a href="/' . $slugList[$i]['slugurl'] . '">' . $slugList[$i]['slugname'] . '</a><br>';
	}
}

What I cant comment on/help with is:

1.) Where you get this ‘list’ of page names (slug names?) initially.
2.) What table you need to point at to get the results (I created a fake table for testing)
3.) Where you output or use this list of links in the end.

Sponsor our Newsletter | Privacy Policy | Terms of Service