navigation: same category AND custom field?
-
Hi,
I’ve found documentation and at least one plug-in for setting up post-to-post navigation within a category. Is it also possible to do post-to-post navigation where I check for the same category AND the existence of a custom field value? This would be navigation for an ongoing narrative and the custom field value would indicate that the post starts a new chapter. I’d like to provide both post-to-post links within the category (the easy part) and chapter-to-chapter links within the category (the part I can’t quite figure out). And this would be for the single post display page. Thanks for any help.
Matt
-
So you want the functionality of next_post_link() and previous_post_link() with the ‘in_same_cat’ parameter set, but also an ability to test for a custom field?
If that’s what you’re looking for, then I like the idea. Let me see if I can cannibalize a couple of plugins of mine and make this work (without too much work on *my* part, that is).
So you want the functionality of next_post_link() and previous_post_link() with the ‘in_same_cat’ parameter set, but also an ability to test for a custom field?
Exactly! I have a habit of taking the long way around to say things. ??
I appreciate you taking a look at it and I’ll check back to see what develops. In the meantime, I’m also studying the codex and dissecting some related plug-ins to see what I can learn.
Matt
Took a bit, but…
Next-Previous Post Meta plugin:
Download plugin | View sourceUsage:
<?php next_post_meta('format', 'link', in_same_cat, 'excluded_categories', 'key', 'value'); ?>
<?php previous_post_meta('format', 'link', in_same_cat, 'excluded_categories', 'key', 'value'); ?>
The first four parameters or arguments are the same as those for next_post_link() and previous_post_link() (read about them in the linked docs). I’ve appended two additional parameters for a custom field key and value.
Example:
<?php next_post_meta('%link', '%title', true, '', 'chapter'); ?>
The above will format the (next post) link with nothing before or after the link, uses the post title for link text, restricts to category, and match posts with a custom field key of ‘chapter’. Note that assigning the ‘value’ parameter is optional (well, unless you need it!).
Man, that Kafkaesqui is resourceful.
Any chance this can be modified to do the same thing, only “next post” is alphabetical instead of chronological?
For purposes of a site you’ve helped me with before, which is a song list with 3 categories, and 4 singers (they’re not the categories). I would like to show a link to the next post title in alphabetical order, by the same singer (same custom field). Another code you made for me already produces a Page which is a list of all the songs sorted by singer (sorted by custom field) – unfortunately I still have that list in chronological order of posting, not in alphabetical, as well – and have been meaning to ask Kafkaesqui about it.
“Any chance this can be modified to do the same thing, only “next post” is alphabetical instead of chronological?
By now you should know the answer to a question like that!
I’ll look over that other plugin you probably know of and see about merging the two.
I checked my theme, and that other plugin isn’t a plugin, I think, it’s just code you gave me (reprinted below). It gives me the results properly sorted-by-custom-field-value (All the songs by Ben, then all the songs by Everyone, then Josh, then Trevor), but the post-titles within each custom-field-value are sorted chronologically, and I want alphabetically (All of Ben’s songs from A-Z, then all of Everyone’s A to Z, etc).
Example of the output,
Index of Songs sorted by lead vocalist* Ben C – Planet
* Ben – Addressing The Community
* Everyone – Panspermic
* Everyone – Mosquito
* Josh Clark – Hot Dog
* Josh Clark – Carter Hotel
* Trevor – Farm’s Been Sold
* Trevor – Watching Anna Fall
* Trevor – Long Way to Texas
* Trevor – ForgivenHere’s the code I use now,
<h3>Index of Songs sorted singer</h3>
<ul>
<?php
if ( $recentposts =
$wpdb->get_results("SELECT ID, post_title, post_date, meta_value, guid FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id WHERE post_status = 'publish' AND meta_key='Lead singer' ORDER BY meta_value, post_title DESC")) {
foreach ($recentposts as $post) {
echo "<li>{$post->meta_value} - <a href="/green/?p={$post->ID}" title="{$post->post_title}">{$post->post_title}</a></li><hr>
";} } ?>
</ul>
<h3>Index of Songs sorted by Album</h3>
<ul>
<?php
if ( $recentposts =
$wpdb->get_results("SELECT ID, post_title, post_date, meta_value, guid FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id WHERE post_status = 'publish' AND meta_key='On CD' ORDER BY meta_value, post_title DESC")) {
foreach ($recentposts as $post) {
echo "<li>{$post->meta_value} - <a href="/green/?p={$post->ID}" title="{$post->post_title}">{$post->post_title}</a></li><hr>
";} } ?>
</ul>You also gave me the plugin for alphabetical Page Navigation, which works perfectly. I guess that’s the one you indicated could be combined to select by custom-field, which would also be handy.
I guess my top need is to fix the sub-alphabetization on the above code; second goal is the page nav idea stemming from this thread. You’re very kind to be so helpful.
I thought about the Page Nav topic a bit more. It’s essentially “What is the next post in alphabetical order, that has the same custom field value for x key?” So that’s what this thread was about from the start, only mine’s alphabetical as it is wont to be.
for me it’s “what is the next song in alphabetical order, that is on the same album as this song”
There are other fine uses for WP besides diary/blog!
As we’re dealing here with a mod to next_post_link and previous_post_link, and you asked “Any chance this can be modified to do the same thing, only “next post” is alphabetical instead of chronological?” the only plugin of mine I could have been referring to was this one.
Like I said, I’ll look at merging the two for what you ask.
Now, for a bit of opinionating…
When using Microsoft Excel to write a novel, which you *can* do, don’t fault the software for making it hard to do that. It’s very true WordPress can be used for something other than just a blog. But WordPress was developed first and foremost as a tool for weblogging, and it’s incumbent upon us to keep that in mind when extending it to other uses.
I mean to praise WP for being mod-able to achieve other uses like Dictionary, Novel, Cookbook, Songs Index. With help from someone who understands how to write the queries, this is totally doable! I’m thrilled with this use I came up with for WP. In a year of hacking around and getting help on this forum, I’ve got the site doing all my minimum requirements, site is functional, at this point we’re just working on little tweaks to make it even more useful for my readers, to push the envelope a little bit. When I feel it is sufficiently near-perfect, I hope to release the theme or at least guidelines on which plugins and queries to use, so that others can do this too.
By the way, if you alter the Next/Previous Post Plugin (that you linked… which I use), I would want to use both versions, the plugin as-is right now is in use on my site. I would place the output of the NEW/modified version, right underneath the output of the current plugin as-is. So it would show both side-by-side:
1. The next post in alphabetical order
2. The next post in alphabetical order with matching custom field value.aka, for me,
1. The next song in order.
2. The next song that is on the same album as this song.This looks very close to what I am trying to do! I want to display my post’s subtitle (a custom field) within the next/previous links.
Previous: title – subtitle
Next: title – subtitleSo far I haven’t found anything with the ability to do this, but Kaf’s plugin comes very close. So any thoughts or input would be appreciated. Thanks!
Any chance to get this plugin work with 2.3?
Any chance to get this plugin work with 2.3?
Hey, forgot about this one! Updated to R.1.1 to support WordPress 2.3…
Thanks! I’ll give it a try.
- The topic ‘navigation: same category AND custom field?’ is closed to new replies.