• Resolved wmburke

    (@wmburke)


    I would like to be able to display a list of links only if they are present in two different link categories.

    For example, I have a list of country names as link categories, and I have a list of link categories that are specific to each country: human rights organizations, political parties, etc. I want to be able to say, “Only show me links are categorized as both human rights organizations and Egypt.”

    Is this possible?

    I am doing a similar thing with blog posts using query_posts in the template just before the Loop as follows:

    <?php query_posts(array('category__and'=>array($country_cat_id,98),'showposts'=>3)); ?>

    Any help would be greatly appreciated. Otherwise, I will need to have 12 categories for each country, and that seems a little ridiculous when multiplied by 26.

    I am open to a direct WordPress oriented way or a clever piece of PHP code that saves the links to an array and then compares and outputs a final list for printing. I know that this can be done, but I haven’t written a sort algorithm in a long time, and don’t really remember the best one to use.

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter wmburke

    (@wmburke)

    Thanks – got it!

    It has taken a bit of SQL learning, but here’s the code that I’m using to limit the links that are displayed to just that fit into two specific categories:

    <?php $link_list = $wpdb->get_results(SELECT wp_links.link_name, wp_links.link_url, wp_links.link_description, wp_links.link_notes
    FROM wp_links
    WHERE wp_links.link_name
    IN (
    
    SELECT wp_links.link_name
    FROM wp_links
    INNER JOIN wp_term_relationships ON wp_links.link_id = wp_term_relationships.object_id
    INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
    WHERE wp_term_taxonomy.term_id = '100'
    )
    AND wp_links.link_name
    IN (
    
    SELECT wp_links.link_name
    FROM wp_links
    INNER JOIN wp_term_relationships ON wp_links.link_id = wp_term_relationships.object_id
    INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
    WHERE wp_term_taxonomy.term_id = '107'
    )
    ORDER BY wp_links.link_notes ASC ");
    foreach ($link_list as $link_list) {?>
    	<li><a href="<?php echo $link_list->link_url ?>"><?php echo $link_list->link_name?></a> <?php echo $link_list->link_description ?></li>
    <?php } ?>

    Now, I know it looks ugly, but here’s what you need to know to put it together.

    1. You need to familiarize yourself with the WordPress Database Tables – you can do that from the codex here, although it really helped me to use PHPMyAdmin also. This is also intensely useful for verifying that the SQL Query (everything from Select to ASC) works right.
    2. The SELECT statement right at the beginning specifies what variables get returned from the links database – you can see that I’m only using the name, URL, and Description.
    3. The section following the first WHERE statement contains multiple selection criteria. The first says that the link must be contained within category ’98’. The second says that the link must be contained within category ‘105’. And the two are connected by an AND statement (You could also use an OR, although this would be easier done with wp_list_bookmarks unless you something complicated).
    4. ORDER BY is the term that the returned list is ordered by (obvious, right?) sorted in ASCending or DESCending order.
    5. The output is just customized html – I made them as list items for my use.

    That’s it – I hope it’s useful to someone else.

    Thanks! I have been looking for something like this, but I have no experience in SQL of CMS. Will try this out ??

    At which page/section did you edit the above?

    Can the same thing be done to normal categories instead of link categories?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘limit displayed links by two categories’ is closed to new replies.