WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom values and categories (5 posts)

  1. Jens Wedin
    Member
    Posted 6 years 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

  2. Jens Wedin
    Member
    Posted 6 years ago #

    * bump *

    (sorry for bumping)

    /Jens

  3. Roger Theriault
    Member
    Posted 6 years ago #

    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>';
    }
  4. Jens Wedin
    Member
    Posted 6 years 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

  5. Roger Theriault
    Member
    Posted 6 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic