JediSthlm
Member
Posted 1 year ago #
Howdy
Is there a way to show links to posts that are within a certain category and have a specific custom value? The links are not within the loop. I searched the forum and googled but not found anything that could help. Anyone knows if this can be done?
/Jens
JediSthlm
Member
Posted 1 year ago #
* bump *
(sorry for bumping)
/Jens
You mean custom field? If that's the case, here's some sample PHP...
This function can go in your theme's functions.php, call it in a template where you need the list.
You'll have to customize it as needed. And add a SQL clause to check the post category.
function my_custom_links() {
global $wpdb;
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'custom_field_key'
AND (wpostmeta.meta_value = 'custom_value_1' OR wpostmeta.meta_value = 'custom_value_2')
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_title ASC
";
$postlist = $wpdb->get_results($querystr);
if (! $postlist) return;
echo "<ul>";
foreach ($postlist as $fp) {
$fp_id = $fp->ID;
echo '<li><a href="' . get_permalink($fp_id) .
'" title="More about ' . $fp->post_title .
'">' . $fp->post_title . "</a><br />";
echo '</li>';
}
echo '</ul>';
}
JediSthlm
Member
Posted 1 year ago #
Thanks a lot, you are a very nice man! That did the trick, from here I can make it work they way I want.
/Jens
You're most welcome. Don't forget to set the forum thread to resolved...
By the way, I am in the process of switching from some custom fields to a new data table... if you plan to do any kind of complex search (such as on more than one custom field at a time) it might be better to use a database table.
Roger