2

I am getting this error when I call this method $query->the_post();. I am trying to get the post id to get the post meta data. Accessing the $post->ID from global post return the current page id, but I need the post id for searching purpose.

You can see the error as image here

function search_form_submit(){

$task_name  = isset($_GET['taskname']) ? $_GET['taskname'] : '';
$meta_key = 'wp_tasks';

$args = array(
                    'post_type' => 'tasks',
                    'post_status' => 'publish',
                    'posts_per_page' => 5,
                    'meta_query' => array(
                        array(
                            'key' => $meta_key,
            'value' => $task_name,
                            'compare' => 'LIKE',
                        ),
                    ),
                );


$query = new WP_Query($args);
global $post;

if($query->have_posts()){

    $query->the_post();

    $data = get_post_meta($post->ID,$meta_key,true);
     }
 }

 $tasks_obj = new tasks();

  if($_SERVER['REQUEST_METHOD'] == 'GET') {
  $tasks_obj->search_form_submit();
 }
7
  • Where is this code being used? What hook? Oct 24, 2017 at 8:00
  • Have a look on links may be helpful to explain why you got this error wordpress.stackexchange.com/a/126206/80247
    – jas
    Oct 24, 2017 at 8:01
  • if you use the while loop, the post ID is not required. It shows meta values for the current post. Just use get_post_meta($meta_key); Oct 24, 2017 at 8:15
  • 1
    For context, get_userdata() is used inside setup_postdata(), which is used inside the_post(). Since it’s pluggabke you can’t use it before plugins_loaded. Hence my question about the hook. Oct 24, 2017 at 8:18
  • 1
    Well you need to. Why don't you want to use a hook. This is WordPress. Everything is hooks. Oct 24, 2017 at 8:22

1 Answer 1

4

Generally, forms should be handled on init action or later (mainly in a plugin file, maybe in the theme's functions.php file, never in a tempalte file), so you can have access to WP functions.

add_action( 'init', 'search_form_submit' );
function search_form_submit(){

    $task_name  = isset($_GET['taskname']) ? $_GET['taskname'] : '';
    $meta_key = 'wp_tasks';

    $args = array(
                        'post_type' => 'tasks',
                        'post_status' => 'publish',
                        'posts_per_page' => 5,
                        'meta_query' => array(
                            array(
                                'key' => $meta_key,
                'value' => $task_name,
                                'compare' => 'LIKE',
                            ),
                        ),
                    );


    $query = new WP_Query($args);

    if($query->have_posts()){

        $query->the_post();

        $data = get_post_meta(get_the_ID(),$meta_key,true);
         }
     }

     $tasks_obj = new tasks();

     if($_SERVER['REQUEST_METHOD'] == 'GET') {
      $tasks_obj->search_form_submit();
     }

}
0

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.