I’ve done it! Now everything is dynamic. I’ll write an article on it after I’m done with the entire project and can show people. For now, here are some snippets. Suggestions for improvement are welcome.
Note: This is all in gallery_template.php in your theme folder.
Variables
global $parent_id;
global $cat_value;
global $query_parent_id;
global $query_parent_title;
I made them global because they are used in and out of if and loops.
$parent_id is what we’re trying to obtain. $cat_value is the query string value. $query_parent_id is the parent id from the SQL query. $query_parent_title is the parent title from the SQL query.
—
Here’s the query I used:
$parents = $wpdb->get_results("SELECT post_title, id FROM wp_posts WHERE (post_parent = 0 AND post_type = 'gallery' AND post_status = 'publish')");
I want to get the post_title and id of the parent albums to use later.
—
Here’s where I’m getting the post_title and id from each array item. Then I’m comparing the parent_title with the query string title. I’m making the parent_title lowercase so they match since the query string is all lowercase but the album title is capitalized. There may need adjustments if say your album has more than one word and your query string uses dashes. Then you can use str_replace on the album title. Unless there’s a better way.
Then if the parent_title matches the query string value, then assign id to $parent_id.
foreach($parents as $parent) {
$query_parent_id = $parent->id;
$query_parent_title = strtolower($parent->post_title);
if(isset($_GET['cat'])) {
$cat_value = $_GET['cat'];
// compare the queried parent_title with the query string value, then assign its id to $parent_id
if($query_parent_title==$cat_value) { $parent_id = $query_parent_id; }
}
}
—
Here’s where I added the post_parent filter, and $parent_id finally comes into good use. It’s saying find all gallery posts that are published and order them by post date, with no set posts per page, and have the same parent id as the one you found before through the query string and id matching.
$args = array(
'post_type' => 'gallery',
'post_status' => 'publish',
'orderby' => 'post_date',
'posts_per_page' => -1,
'post_parent' => $parent_id
);
—
I forgot, you also need to have a page for the parent albums and link the albums to
?cat=album
and where album is is the name of your album. This page you’ll have to make yourself instead of with the auto-generated. I’m assuming there’s a more automatic way to do this if you can write a program for it, but for now this is as far as I got.
Feel free to share your ideas!