Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following function that successfully uses form fields (IDs 13 and 18) from a Gravity Form (ID 18) as parameters within wp_query. This displays a list of posts with specific taxonomy terms that match those chosen in the form:

add_action("gform_after_submission_18", "set_post_content", 10, 2);
function set_post_content($entry, $form){

    //getting post
    $post = get_post($entry["post_id"]);
    $concern = $entry[13];
    $sensitivity = $entry[18];

    $loop = new WP_Query( array( 
        'post_type' => 'recommended',    
        'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'concern',
            'field' => 'slug',
            'terms' => $concern
        ),
        array(
            'taxonomy' => 'sensitivity',
            'field' => 'slug',
            'terms' => $sensitivity
        )
        ),

        'orderby' => 'title',
        'posts_per_page' => '-1', 
        'order' => 'ASC'
      ) 
   ); ?>

//THIS BIT DISPLAYS THE CORRECT LOOP BUT IT APPEARS IMMEDIATELY AFTER <BODY> RATHER THAN IN THE POST CONTENT
   <ul><?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
   <li><?php the_title(); ?></li>
   <?php endwhile; ?></ul>
   <?php 


//THIS BIT DISPLAYS THE CHOSEN FORM FIELD INSIDE MY POST CONTENT - WRONG CONTENT, RTIGHT POSITION
 $post->post_content = 
"<ul>" . 
$concern . 
"<br/> sensitivity: " . 
$sensitivity . 
" </ul>"
;

 }

The problem is that this is being inserted immediately after the 'body' tag rather than into the post body. How do I move this to where it should be?! I could well be going about this all wrong and I'm stumped

share|improve this question

closed as off-topic by Mark Kaplun, ialocin, mrwweb, Pieter Goosen, G. M. Aug 31 at 6:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

    
I don't know what the "post/page div" is. That could be something created by the theme or maybe you mean that you want this information in the post body? –  s_ha_dum Apr 15 '13 at 14:00
    
Yes, sorry - that is what I mean. I need it to appear in the post body. I've updated my question. –  Madebymartin Apr 15 '13 at 15:07
    
I don't have time to try to answer this but if I wanted something in the post body the first thing I think about is the the_content filter, but given that this is Gravity Forms perhaps it doesn't work that way. –  s_ha_dum Apr 15 '13 at 15:11
    
First, there is a missing question mark in your last <?php. Second, (why) are you able to access the variables $concern and $sensitivity? Is there some other code (that is not shown here) where they are set, defined global, ...? –  t f Apr 16 '13 at 15:59
    
Whoops I think the missing question mark is somehow where I copy / pasted to here.. Not in my original code and I've updated it here.. The variables are set at the top by the Gravity Form fields. –  Madebymartin Apr 16 '13 at 19:44

2 Answers 2

Just thinking on a Friday night (so untested), but:

You are outputing the result directly to the page. Of course it is going to display straight after the body tag. What you need to do is store that HTML snippet, and append/insert into the content using the_content filter as s_ha_dum suggested.

This is untested, and typed after a few Pernods, but:

class wpse_95891 {

    protected $post_content;

    public function __construct() {
        add_action("gform_after_submission_18", array($this, "set_post_content"), 10, 2);
        add_filter("the_content", array($this, "the_content"));
    }

    /**
    * @param array $entry the Gravity Forms entry/lead "object"
    * @param array $form the Gravity Forms form "object"
    */
    public function set_post_content($entry, $form) {
        // insert all of your query logic here...

        // grab the output and store in a field for later
        ob_start();
        ?>
        <ul><?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <li><?php the_title(); ?></li>
        <?php endwhile; ?>
        </ul>
        <?php
        $this->post_content = ob_get_clean();
    }

    /**
    * @param string $the_content post/page content
    * @return string
    */
    public function the_content($content) {
        if (!empty($this->post_content)) {
            $content .= post_content;
        }

        return $content;
    }
}

new wpse_95891();
share|improve this answer
    
No need for ouptut buffering. :) –  toscho Apr 19 '13 at 10:42
    
True, but I wasn't going to rewrite the OP's loop logic (not with this much Pernod in me!) –  webaware Apr 19 '13 at 10:45
    
But … Pernod is Coetry! –  toscho Apr 19 '13 at 10:48
    
Crikey, that made sense, which must mean I need another... –  webaware Apr 19 '13 at 10:50

thanks for all your input.. I just managed to achieve what I needed (with massive thanks to Alex at WPMU!), using global declarations -

Probably not the most elegant solution but it does what I need. This is actually a much simplified version of what I'm doing and this way I know how to make it work!

Please do point out any issues with what I've done if there are any!?

add_action("gform_after_submission_18", "recommendations", 10, 2);
function recommendations($entry, $form){

global $concern;
global $sensitivity;

$post = get_post($entry["post_id"]);
$concern = $entry[13];
$sensitivity = $entry[18];

add_filter("the_content", "recommended");
}





function recommended() {

global $concern;
global $sensitivity;

echo 'Here are your recommendations, based on your chosen options: <br>';
echo 'Sensitivity: ';
echo $sensitivity;

echo '<br>Concern: ';
echo $concern;
echo '<br><br>';


$loop = new wp_query( array( 
    'post_type' => 'recommended',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'concern',
            'field' => 'slug',
            'terms' => $concern
        ),
        array(
            'taxonomy' => 'sensitivity',
            'field' => 'slug',
            'terms' => $sensitivity
        )
    ),
    'orderby' => 'title',
    'posts_per_page' => '-1', 
    'order' => 'ASC'
) );



while ( $loop->have_posts() ) : $loop->the_post(); ?>

<div class="">
<?php the_title(); ?><br>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail("thumb"); } ?>
</div>

<?php endwhile; 
} 
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.