• Resolved phpchick

    (@phpchick)


    When I hardcode the value in, like I’ve done below. “Healthcare”, “Technology”, etc. My posts show up. However, this needs to be dynamically done via the URL GET Variable.

    $args = array(
    	'post_type' => 'earnings', // use to be stock
    	'posts_per_page' => 1,
    	'offset' => $_GET['offset'],
    		'meta_query' => array(
                            array(
    			'key' => 'sector',
    			'value' => 'Healthcare'
    			))
    );

    However, when I do this.

    $args = array(
    	'post_type' => 'earnings', // use to be stock
    	'posts_per_page' => 1,
    	'offset' => $_GET['offset'],
    	'meta_query' => array(
                array(
    			'key' => 'sector',
    			'value' => $_GET['sector'],
    			'compare' => '='
    			))
    );

    The code busts and I do not get any results. I’ve tried setting

    $_GET[‘sector’] to a variable first, and then doing
    'value' => $variable
    but that didn’t work either, and I have echoed that variable out to the screen and it indeed is “Healthcare”.

    I’m not sure what I’m doing wrong, could anyone lend a helping hand?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Try var_dumping $_GET and paste here (if it isn’t too long) the returned code.

    Thread Starter phpchick

    (@phpchick)

    I did

    var_dump($_GET['sector']);

    and out went

    string(10) “Financials”

    Thread Starter phpchick

    (@phpchick)

    And here is %_GET by itself

    array(5) {
    [“category”]=> string(5) “beats”
    [“date”]=> string(1) “4”
    [“sector”]=> string(10) “Financials”
    [“expected_deviation”]=> string(8) “Moderate”
    [“expected_outlook”]=> string(7) “Neutral”
    }

    Thread Starter phpchick

    (@phpchick)

    Could this be an issue with the database?

    I feel like the above should work

    typing in the string works, but the identical string given through a variable doesn’t. The var_dump suggests that they are identical from what I can see. Anyone have any insight?

    You need to first register your new variable before WordPress will recognise & use it. See this Codex section.

    Thread Starter phpchick

    (@phpchick)

    Just want to follow up so others can use the code if they need help.

    I was able to fix the code thanks to esmi and here it is

    add_filter('query_vars', 'geturlvar' );
    function geturlvar( $qvars )
    {
      $qvars[] = 'sector';
      return $qvars;
    }
    
    $args = array(
    	'post_type' => 'earnings', // use to be stock
    	'posts_per_page' => 3,
    	'offset' => $_GET['offset'],
    	'meta_query' => array(
                array(
    			'key' => 'sector',
    			'value' => $_GET['sector']
    			))
    );

    Thank you so much esmi! <3

    Glad I could help ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘simple syntax issue of hardcoded value vs GET variable’ is closed to new replies.