1

I'm getting the following error "Notice: Undefined index:" on 'name' => $_GET['channel'].

When there's a parameter it works and allows the loop to proceed, but when there's no parameter set then I get the error above.

If I try isset it then removes the error but stops the wordpress loop from proceeding if a parameter is set.

What am I missing?

<?php
$args = array (
'post_type' => 'abc_channels',
'name' => $_GET['channel'],
'post_status' => 'publish',
'posts_per_page' => 1,
);

    $loop = new WP_query( $args );

    // If we have live channels 
    if (isset($_GET['channel'])) : 
    if($loop->have_posts()): 

    while($loop->have_posts()): $loop->the_post(); 
    ?>

1 Answers1

0

Thanks to @aynber for pointing me to a solution.

<?php $channelvalue = ""; //Initialization value; Examples
//"" When you want to append stuff later
//0  When you want to add numbers later
//isset()
$channelvalue = isset($_GET['channel']) ? $_GET['channel'] : '';
//empty()
$channelvalue = !empty($_GET['channel']) ? $_GET['channel'] : '';
?>

<?php
    $args = array (
    'post_type' => 'abc_channels',
    'name' => $channelvalue,
    'post_status' => 'publish',
    'posts_per_page' => 1,
    );

    $loop = new WP_query( $args );

    // If we have live channels 
    if (isset($_GET['channel'])) : 
    if($loop->have_posts()): 

    while($loop->have_posts()): $loop->the_post(); 
    ?>