• $args = array(
    	'post_type' => 'earnings', // use to be stock
    	'posts_per_page' => 1,
    	'offset' => $_GET['offset'],
    );
    
    $the_query = new WP_Query( $args );
    while ( $the_query->have_posts() ) : $the_query->the_post();
    ?>

    I have a section of my website that is suppose to display some posts from post_type “earnings”.

    However, I want to show results from posts where the custom field ‘sector’ is equal to a value taken from a GET variable in the URL.

    Does anyone know how to modify the above code? I’ve tried a lot of things but have not been successful.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter phpchick

    (@phpchick)

    So I’m starting to figure it out.

    I changed the code to below and it works. However, obviously, the value is hard coded in.

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

    If I try to have it pull from the GET variable in the URL like below, it does not work.

    $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']
    			))
    );

    At this point I’m sure it is a layup, can anyone see any syntax issues?

    Thread Starter phpchick

    (@phpchick)

    $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' => '='
    			))
    );

    It still doesn’t work even with the compare line

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Displaying posts where a certain custom field is equal to a GET variable’ is closed to new replies.