WordPress.org

Support

Support » Plugins and Hacks » [Resolved] custom field query

[Resolved] custom field query

  • All of my articles in category “A” have a custom field whose numeric value is equal to ID’s of the posts in category “B”.

    Now I’d like to link these and show links to all the posts from “A” at the bottom of appropriate post in “B”.

    Any ideas?

    Tnx in advance.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Not sure what your custom field name is but will assume it is ‘related’, put this inside your existing loop

    <?php
    $related_post = get_post_meta($post->ID, 'related', true); //assumes custom field key is 'related'
    if ($related_post) {
      $args=array(
        'post__in' => array($related_post),
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo 'Related Post';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        endwhile;
      }
      //wp_reset_query();  // Restore global post data stomped by the_post().
    } //if related
    ?>

    MichaelH, thank you so much!

    I’ll test this ASAP and let you know if it works…

    Tnx again!

    nope. 🙁 it returns nothing 🙁 shouldn’t the code somwhere include the category I want to query?

    If I read the question right, what you want is to display, under post ‘B’, links to any posts that have B’s post ID in their custom field. I am assuming that you want to link to any posts that have ‘B’ ids in the custom field, not only ‘A’ posts.

    If that is correct, this may work. Paste the code below to the end of your theme’s function.php. Then add a call to the function somewhere after the display of the post content.

    <?php function get_related_post_links() {
    /* List links for posts that have the current post's id in a Custom Field's value
       Multiple post id's in a custom field must be comma separated, e.g. 22,51,1,35.
    Author: Mac McDonald
    Contact: Use 'Contact Us' form under 'About' at BluegrassMiataClub.com
    */
    global $wpdb;
    $custom_field = 'related';  // Change this to your Custom Field name.
    $post_id = get_the_ID();
    $query = "SELECT * FROM $wpdb->postmeta WHERE meta_key = '$custom_field' AND meta_value like '%$post_id%'";
    $rows = $wpdb->get_results($query,OBJECT);
    if ($rows) {
       $list_start = '<div class="relatedposts"><p>Related Posts:</p><ul>';
       foreach ($rows as $row) {
          // We may have gotten spurious values e.g. like '%9%' will return '9', '19', '29', etc
          // so we weed them out here.
          $value = ',' . $row->meta_value . ',';
          $pattern = '/, *'. $post_id . ',/';
          if (preg_match($pattern, $value)) {
             $related_id = $row->post_id;
             $related_title = get_the_title($related_id);
             // Get the permalink for the related post and echo it
             echo ($list_start . '<li><a href="' . get_permalink($related_id) .'" title="' . $related_title . '">' .
                $related_title . '</a>');
             $list_start = '';
          }
       }
       if ($list_start == '') echo '</ul></div>';
    }
    }?>

    nope. I get “Cannot modify header information – headers already sent by” if I paste this into my functions.php

    Perhaps you left a blank line at the end of functions.php, or just before the code I posted.

    tnx vtxyzzy it’s working now… alas I have one more request… for reasons unknown to me this piece of code lists even the articles that have been erased from the database… do you have any ideas how to change that?

    I think this is the query you want, but I have not tested it:

    $query = "SELECT m.*
    FROM $wpdb->postmeta m, $wpdb->posts p
    WHERE m.meta_key = '$custom_field'
    AND m.meta_value like '%$post_id%'
    AND m.post_id = p.ID
    AND p.post_type = 'post'
    AND p.post_status = 'publish'
    AND p.post_date < NOW()";
    $rows = $wpdb->get_results($query,OBJECT);

    vtxyzzy – problem solved… the first query was fine, however it looped trough the posts marked ‘trash’. now I have to remember to empty trash regularly…

    tnx so much!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] custom field query’ is closed to new replies.
Skip to toolbar