5

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.

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.

1 Answer 1

5

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.

3
  • Want to be pedantic and annoying: conditional tags start working on pre_get_posts :)
    – gmazzap
    Sep 22, 2013 at 1:57
  • @G.M. They can still be overwritten there.
    – fuxia
    Sep 22, 2013 at 2:04
  • nice thanks. i don't know why i didn't see those events when i looked... probably just dumb. thanks! Sep 22, 2013 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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