is_page() not working from within a plugin

In my plugin I’m doing:

function get_user_strains()
{
    $userID = get_current_user_id();

    $args = array(
        'post_type'     => 'strain',
        'orderby'       => 'post_date',
        'order'            => 'DESC',
        'post_status'      => 'publish',
    );

    $strains = get_posts($args);

    global $userStrains;
    $userStrains = array();

    foreach($strains as $i)
    {
        $meta = get_post_meta($i->ID);
        //var_dump($meta);
        if($i->post_author == $userID)
        {
            $i->type = $meta['type'][0];
            $i->rating = $meta['rating'][0];
            //var_dump($i);
            array_push($userStrains, $i);
        }
    }

    //var_dump($userStrains);
    $userStrainsJson = json_encode($userStrains);

    //var_dump($userStrains);

    wp_reset_query();
}
add_action('init','get_user_strains');

function edit_strain()
{
    if(is_page()) echo "IS A PAGE";
    $id = isset($_GET['id']) ? $_GET['id'] : 'no id set';

    global $editStrainData;
    $editStrainData = get_post($id);
    global $editStrainMeta;
    $editStrainMeta = get_post_meta($id);

    wp_reset_query();
}
add_action('init','edit_strain');

I’m trying to determine if a custom template I made is a page. Using or not using “The Loop” in my template page does not make a difference.

Read More

In edit_strain() is_page() is returning false. Does it have something to do with me using get_posts in get_user_strains()? I’ve heard I should be using WP_Query() but I don’t really know how to convert that get_posts() into a WP_Query().

Would this be the problem?
I have searched Stack Exchange and couldn’t find anything that solved my issue.

Related posts

1 comment

  1. The action init happens too early to know if the current page should display a page. You have to wait at least until template_redirect. But if you want to print something to the page, use the_content.

Comments are closed.