WordPress.org

Ready to get started?Download WordPress

Forums

Advanced Custom Bookmark Query (4 posts)

  1. fanalejr
    Member
    Posted 5 years ago #

    Hey all,
    I've been looking around here for a couple of days now. I know the codex wp_list_bookmarks, and I found Otto's query below:

    <?php
      $taxonomy = 'link_category';
      $title = 'Link Category: ';
      $args ='';
      $terms = get_terms( $taxonomy, $args );
      if ($terms) {
        foreach($terms as $term) {
          if ($term->count > 0) {
            echo '<p>' . $title .' ID = '. $term->term_id . ' ' . $term->name . ' contains ' . $term->count . ' link(s). </p> ';
          }
        }
      }
    ?>

    (which i thought was pretty cool)
    If someone has a way to accomplish the following, it'd really help me out:
    I have a set of pages set up to use the same template (issues.php) Now, i have it all dynamically set up so that the template knows which page it is and it calls in posts related to that page (by a custom field selection) using a custom query. My hope was to have a section of related "outgoing links" on the pages as well...

    So I need a way to query the database, check to see if there are links in the necessary category. If there are, create a wrapper div that will hold the list of links.

    I hope that makes sense, Here is an example of the code i'm using to pull in related posts, I guess I need something like this, but querying the links instead?

    <?php
    /* QUERY FOR RELATED BLOG POSTS */
     $querystr = "
       	SELECT * FROM $wpdb->posts
    	LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    	LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    	LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    	WHERE $wpdb->term_taxonomy.term_id = 15
    	AND $wpdb->term_taxonomy.taxonomy = 'category'
    	AND $wpdb->posts.post_status = 'publish'
    	AND $wpdb->postmeta.meta_key = '$issueCategory'
    	AND $wpdb->postmeta.meta_value = 'on'
    	ORDER BY $wpdb->postmeta.meta_value ASC
    	LIMIT 4
    	";
    ?>
    
    <?php /* RELATED BLOG POSTS DISPLAY */ ?>
    <?php $pageposts = $wpdb->get_results($querystr, OBJECT);
    if ($pageposts): ?>
    <div id="recentPosts" class="module">
    	<div class="title">Related Posts</div>
    <?php  foreach ($pageposts as $post):
    ... so on and so forth

    Thanks in advance to anyone who can point me in the right direction

  2. fanalejr
    Member
    Posted 5 years ago #

    Hey guys, sorry to bump, this is just all I got left before sites completed.
    Let me know if you can help,
    thanks

  3. fanalejr
    Member
    Posted 5 years ago #

    Ok, so I figured out a way to do what I needed with the bookmarks, I'm going to post it here just in case it helps anyone in the future.

    First off, here is the code:

    <?php
    $bookmarks = array();
    $bookmarks = get_bookmarks("category_name=$issueCategory");
    if ($bookmarks[0] != '') { ?>
    <div id="links" class="module">
        <div class="title">Related Links</div>
    <?php
    foreach ( $bookmarks as $bookmark ) {
    ?>
    <ul>
    <li><a title="<?php echo $bookmark->link_name; ?>" href="<?php echo clean_url($bookmark->link_url); ?>" target="_blank"><?php echo $bookmark->link_name; ?></a></li>
    </ul>
    <? } ?>
    </div>
    <?php } ?>

    The way this is working is that we first assign the variable $bookmarks as an array, then we run a query on the bookmarks where, in this case, the category name equals $issueCategory. I have it set up so that $issueCategory is assigned the post name, and I have link categories that equal certain post names. Its kind of a way to manually select "related links." It checks to see if there are any, and the "does something"

    If anyone has a better/easier way of doing this, let me know, but this worked out really well for me.

  4. mircury777
    Member
    Posted 5 years ago #

    This works for me except when I view a category that doesn't have a corresponding bookmark category. It lists everything. Is this working for you like that as well?

Topic Closed

This topic has been closed to new replies.

About this Topic