• Resolved 3stripe

    (@3stripe)


    Hello people… I’m very new to WordPress, could anyone give me some quick thoughts on something…. I don’t need an exact solution, but would be interested to know if this would be possible to do something with custom fields, or not:

    For articles posted to a category called ‘Events’, add a custom field called ‘DJs’, and then add a list of DJs playing to this custom field. (How should you list multiple entries?)

    Then in a static page for each DJ, there could be an area listing ‘Events Played At’, which would automatically list any entry in ‘Events’ for which the custom field contained their name, and display a link to this event.

    Thankyou ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • Answering your primary question, you could certainly use custom fields for this. However, when I’m looking for a solution I want it to be exact. ;)

    For multiple DJ ‘s tied to an event, you can use your “DJ” custom key multiple times, providing the different DJ names for each value ( in regards to querying them later, this is mildly smarter than combining under a single custom field entry).

    On the DJ Pages you would need a block of PHP code that queries the postmeta table for entries with a key of “DJ” and value of the DJ’s name (note: using the same name for the value as for the Page ‘slug’ name would simplify your scripting). Each postmeta record holds the post (i.e. your event) ID, which you can use to retrieve the permalink to the post, or by using something like my plugin Get-a-Post, display each post’s title, date, excerpt…whatever.

    Thread Starter 3stripe

    (@3stripe)

    Hey, thanks for the pointers, the only reason I didn’t ask for exact info is that it spoils the thrill of the chase ??

    Anyhoos, I think that sounds about right for my skillset just now!

    To clarify one point – are you suggesting I should slug all the Events postings with ‘DJ’, or should I slug each DJ page with the DJ’s name?

    Clarification is good. By ‘slug’ I mean the url form of a Page’s name, where “About My Site” becomes “about-my-site”. So with a Page named for a DJ, you’d have “john-smith” (or “john”, dj-john”, whatever) as a Page slug.

    (In the WordPress administration, go to Manage > Pages and then edit a Page, and you’ll find the “Page slug” field.)

    With custom permalinks set up, you can use the following to query that Page *name* in your script:

    get_query_var('name'))

    Assigning this to a variable to match against custom field values (and using the Page slug names for those custom field values), you could do a simple comparison instead of managing a collection of if/else or switch statements to test against for each DJ.

    Thread Starter 3stripe

    (@3stripe)

    Sounds perfect!

    Thankyou for your time Kafkaesqui.

    Thread Starter 3stripe

    (@3stripe)

    Kaf, I’ve finally made time to come back to this – but am really stumped and think this is gonna be way way above my head….

    Has anyone written a plugin that will help me with this?

    Cheers

    3stripe

    Thread Starter 3stripe

    (@3stripe)

    Hmmm, I’m wondering if the source code of https://erwin.terong.com/2005/09/24/wp-plugin-related-posts-link/ holds any answers for my quest… (puts on ‘serious’ hat)

    I’ve found 2 functions so far that are giving me clues… not sure what to do to them next though:

    function get_all_meta($key, $single = false)
    {
    global $wpdb, $post_meta_cache;
    $metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '$key'", ARRAY_N);
    $values = array();
    if ( $metalist ) {
    foreach ($metalist as $metarow) {
    $values[] = $metarow[0];
    }
    }
    if ( $single ) {
    if ( count($values) ) {
    $return = maybe_unserialize( $values[0] );
    } else {
    return '';
    }
    } else {
    $return = $values;
    }
    return maybe_unserialize($return);
    }
    ?>
    /////////////////////////////////////
    /////////////////////////////////////
    /////////////////////////////////////
    function get_related_ids($post_id) {
    global $wpdb;
    $sql = "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE meta_key in('related_post_name', 'related_id') AND post_id=$post_id";
    $searches = $wpdb->get_results($sql);
    if($searches) {
    $post_name = array();
    $post_id = array();
    foreach($searches as $s) {
    if($s->meta_key=='related_post_name') {
    $arr = explode(',', $s->meta_value);
    foreach($arr as $a) $post_name[] = ("'".trim($a)."'");
    }
    elseif($s->meta_key=='related_id') {
    $arr = explode(',', $s->meta_value);
    foreach($arr as $a) $post_id[] = ("'".trim($a)."'");
    }
    }
    $text_post_name = implode(', ', $post_name);
    $text_post_id = implode(', ', $post_id);
    $sql = "SELECT DISTINCT post_title, ID FROM $wpdb->posts WHERE ";
    $arr_sql = array();
    if($post_name) $arr_sql[] = "post_name in($text_post_name)";
    if($post_id) $arr_sql[] = "ID in($text_post_id)";
    $sql .= implode(" OR ", $arr_sql);
    $sql .= " ORDER BY ID DESC";
    $searches = $wpdb->get_results($sql);
    return($searches);
    }
    else return array();
    }

    Thread Starter 3stripe

    (@3stripe)

    Ah ok! I found a readymade solution:

    https://codex.www.ads-software.com/Displaying_Posts_Using_a_Custom_Select_Query

    After searching google for about 2 hours I found this… god only knows why it’s so hidden away… hopefully this will help someone else with similar questions anyhow)

    @3stripe : guess you had the idea for this solution I’ve found months after the fact. Code for what I think you were after here can be found at:
    https://www.ads-software.com/support/topic/73513?replies=22#post-385388

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘List articles containing specific entries to a custom field?’ is closed to new replies.