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.

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.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
Want to be pedantic and annoying: conditional tags start working on pre_get_posts :) –  G. M. Sep 22 '13 at 1:57
    
@G.M. They can still be overwritten there. –  toscho Sep 22 '13 at 2:04
    
nice thanks. i don't know why i didn't see those events when i looked... probably just dumb. thanks! –  Timothy Owen Sep 22 '13 at 23:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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