• I am not sure how to write this query.

    I want this query in sidebar template, to query a particular post (eg ID=10) which has a custom field group(has multiple values) .

    To make sense – here is the how the content post looks like.
    Name: Local programs
    Content: blah……
    Custom field group: Event
    event_name = evt1
    event_name = http://www.evt1.com

    event_name = evt2
    event_name = http://www.evt2.com

    How can I get this query done in sidebar.php ?

Viewing 7 replies - 1 through 7 (of 7 total)
  • You can easily get the post content by using:
    get_post($post->ID);

    And as for your custom fields, you can get them with this function:
    get_post_custom($post->ID);

    Thread Starter ashi01

    (@ashi01)

    Thank you for the response.

    I did try them earlier but don’t seem to get the custom fields.

    Here is the code –

    global $post;
    $post->ID = 10;
    $post = get_post($post->ID);
    echo the_title();
    echo $post->ID;
    if (have_posts()) :
    	$custom = get_post_custom($post->ID);
    	$events = get_group('event');
    	if ($events) {
    	       foreach ($events as $event) {
    			echo $event['event_link'][1];
    			echo $event['event_name'][1];
    		}
    	}
    endif;

    The above code displays the title and the ID , but doesn’t display the custom fields. What am I missing here?

    [Please post code snippets between backticks or use the code button.]

    Are you using the Advanced custom fields plugin? Because i’m not familiar with the get_group() function. Next to that; you’re getting your custom fields, but you aren’t looping through the results. Try:

    $custom = get_post_custom($post->ID);
    foreach($custom as $event){
       echo $event;
    }

    Next to that, why do you check for posts (if->have_posts()) AFTER you’ve echoo’ed the title and post ID?

    Thread Starter ashi01

    (@ashi01)

    The output of $event just dumps everything in the custom fields together as follows-
    Array ( [0] => 1 ) Array ( [0] => 1319043854:1 ) Array ( [0] => 2 ) Array ( [0] => Central Valley [1] => Fresno ) Array ( [0] => http://kpnet.kp.org/centralvalley/wellness/index.html [1] => http://insidekp.kp.org/california/fresno/services/livewellbewell/index.html ) Array ( [0] => default ) Array ( [0] => a:0:{} )

    I am using the Magic Fields Plugin to add custom fields to a post.
    get_group(‘groupname’) is a method to retrieve values that are grouped together.

    This group event has 2 fields – name and link. How can I retrieve it as a name/value pair?

    eg.
    Post:Programs
    Group: Event (can be multiple groups)
    name:event1 link:www.event1.com
    name:event2 link:www.event2.com

    I’m not familiar with that plugin, i think your best guess is to find if the pluing has a community or ask the plugin’s creator.

    Thread Starter ashi01

    (@ashi01)

    I apologize, my mistake. I published the content and it worked. I had updated content and it was saved as a draft earlier, and hence wasn’t getting the events.

    Here is the updated code –
    GLOBAL $post;
    $post->ID = 10;
    $post = get_post($post->ID);
    //== GET CUSTOM DATA
    $custom = get_post_custom($post->ID);
    $sections = get_group(‘event’); // uses multiple values
    if ($sections) {
    foreach ($sections as $section) {
    echo $section[‘event_link’][1];
    echo $section[‘event_name’][1];
    }
    }

    Appreciate your help.
    Thanks,
    Anitha

    hi,
    sort of similar problem
    i have custom field city for multiple posts and on that field i want to have group by query,so that only hyper linked city names should show and on click it should show all posts those lie in that city.
    query_posts('cat=17&groupby=City'); not worked

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Query custom fields’ is closed to new replies.