0

I have a function that is grabbing something from database using a user-defined ID. I need to show the content after every post/page content. So I followed @s_ha_dum's solution from WPSE, like below:

function get_my_content( $id ) {
    $my_content = get_post( $id );
    return get_post_meta( $my_content->ID, 'wp_c_field', true );       
}

$ad_func = get_my_content(1817);    
echo apply_filters('the_content', $ad_func );

Suppose the value in CF 'wp_c_field' is "this is the text". The code above, instead of each post content, it's echoing the data after <body> tag as a paragraph like:

<body>
<p>this is the text</p>

How can I proceed with this things, so that with a direction I can change the hook so that the content can be echoed before content or after content etc.?

EDIT

Please note that, the first function can get post from different post type and can have a different post ID too. So something like:

function my_content( $content )
   global $post;
   $cfv = get_post_meta( $post->ID, 'wp_c_field', true );
   $content = $content . $cfv;
   return $content;
}
add_filter( 'the_content', 'cfv_post_content' );

inside a binding function for the_content won't work for me. As Robert hue suggested as the first answer. I need two parameter to be passed at least.

0

1 Answer 1

2

If I understand well, you want to append to every post a meta value taken from another post, but the latter need to be dynamic, so not hardcoded in the function.

In OP you say the post id is "user-defined". What does it exactly mean? If it is stored somewhere or it is given as query var the simplest solution is moving the code that retrieve the selected post id inside the function:

function get_my_content( $content ) {
  // assuming post id is saved as option
  $my_post_id = get_option( 'selected_post' ); 
  $my_post = get_post( $my_post_id );
  $my_content = get_post_meta( $my_content->ID, 'wp_c_field', true );  
  return $content . $my_content;     
}
add_filter( 'the_content', 'get_my_content' );

or

function get_my_content( $content ) {
  // assuming post id is passed as query var: example.com?thepost=xxx
  $my_post_id = filter_input( INPUT_GET, 'thepost', FILTER_SANITIZE_NUMBER_INT );
  $my_post = get_post( $my_post_id );
  $my_content = get_post_meta( $my_content->ID, 'wp_c_field', true );  
  return $content . $my_content;     
}
add_filter( 'the_content', 'get_my_content' );

There are different ways an user can choose a post id, not knowing exactly how do you select the post I can't be more specific, but take into account the generic suggestion to move the retrieval of post id inside the callback.

If you, for any reason, can't edit the callback to move the post id retrieval inside it, but you have the selected id saved in a variable, a solution can be use a PHP 5.3+ closure and use statement:

// your original function
function get_my_content( $id ) {
    $my_content = get_post( $id );
    return get_post_meta( $my_content->ID, 'wp_c_field', true );       
}

$selected_id = 1817; // user-selected id, stored in a variable

add_filter( 'the_content', function( $content ) use( $selected_id ) {
  return $content . get_my_content( $selected_id );
} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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