• Resolved 101Phil

    (@101phil)


    After doing some research I was advised that I should use get_posts to retrieve a list of pages that meet a set criteria. I have the following query:

    Though instead of pulling out the pages that have the Partner_Level = Gold key value pair, I get all the pages in my site retrieved.

    Where am I going wrong?

    function the_partners() {
    	 $args = array(
    	 			 'meta_query' => array(
    	 			       array(
    	 			        'meta_key' => 'Partner_Level',
    	 			        'meta_value' => 'Gold' ),
    	 					'post_type' => 'page',
    	 					'post_status' => 'publish' ));
    	 			$pages = get_pages( $args );
    
    	$pages = get_pages( $args );
    
    			foreach ( $pages as $page ) {
    			  	$option = '<option value="' . get_page_link( $page->ID ) . '">';
    				$option .= $page->post_title;
    				$option .= '</option>';
    				echo $option;
    		 	  }
    
    }

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently dmaged/corrupted by the forum’s parser.]

Viewing 10 replies - 1 through 10 (of 10 total)
  • A quick reading of this
    http://codex.wordpress.org/Function_Reference/get_pages
    tells me that your second statement should read:

    $args = array(
    'meta_key' => 'Partner_Level',
    'meta_value' => 'Gold',
    'post_type' => 'page',
    'post_status' => 'publish');

    Thread Starter 101Phil

    (@101phil)

    Thanks mate,

    When I use that statement Im not returning any results… See function here.

    function the_partners02() {
    	 // $args = array(
    	 // 		 	 		'meta_query' => array(
    	 // 		 	 			       array(
    	 // 		 	 			        'meta_key' => 'Partner_Level',
    	 // 		 	 			        'meta_value' => 'Gold' ),
    	 // 		 	 					'post_type' => 'page',
    	 // 		 	 					'post_status' => 'publish' ));
    	 // 		 	 			$pages = get_pages( $args );
    
    	$args = array(
    		'meta_key ' => 'Partner_Level',
    		'meta_value' => 'Gold',
    		'post_type' => 'page',
    		'post_status' => 'publish'
    		);
    
    	$pages = get_pages( $args );
    foreach ( $pages as $page ) {
    			  	$option = '<option value="' . get_page_link( $page->ID ) . '">';
    				$option .= $page->post_title;
    				$option .= '</option>';
    				echo $option;
    		 	  }
    
    }

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently dmaged/corrupted by the forum’s parser.]

    Thread Starter 101Phil

    (@101phil)

    Ps I have removed the ) after the ‘meta_value’ => ‘Gold’ ), in my commented out query, saved tested and this didn’t do it either.

    Not sure if it matters, but it might, so please correct this error:
    ‘meta_key ‘ should have the trailing blank removed.

    Thread Starter 101Phil

    (@101phil)

    No that doesn’t seem to do it,,

    If I change the function to use get_posts() in place of get_pages, I get all the posts in the install.. So it seems that my query is not working..

    $pages = get_posts($args);

    I haven’t used Meta before, so I’m not the best one to ask. Only idea left is to turn debug on: WP_DEBUG to TRUE in wp-config.php. And run get_pages with the $args I gave in my first post.

    I just want to see if it is truly finding no pages that match, or returning an error.

    Thread Starter 101Phil

    (@101phil)

    thanks Jon, I will take a look later this afternoon, Im onsite at the moment and I don’t have access to my Git repo to clone a version to this computer.

    Thread Starter 101Phil

    (@101phil)

    Hi Jon,

    It is truly returning no results.. Im not erroring at all…

    Hmm strange one

    According to this post, you should try wp_list_pages:
    http://wordpress.org/support/topic/get-pages-with-meta_key-meta_value?replies=4

    I don’t understand what he’s saying, but this guy says that you have to use WP_Query:
    http://wordpress.stackexchange.com/questions/52967/meta-key-meta-value-not-working-with-get-pages-and-custom-taxonomy/52979#52979

    At some point, you may just have to bite the bullet and use phpMyAdmin to first see how WordPress stores things in its tables in the database, then write the appropriate SQL SELECT statement to retrieve what you want.

    Thread Starter 101Phil

    (@101phil)

    Hi Jon,

    Yeah thats the approach I took, I went back to my original select statement and voila it worked. I will go back to review why the get_pages approach didn’t work, everything looked right from the advice I got here and over at StackOverflow, so its got me wondering thats for sure..

    Thanks for you help…

    function the_partners($cpLevel) {
    	global $wpdb;
    
    	 $querydetails = "
    	   SELECT wposts.*
    	   FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    	   WHERE wposts.ID = wpostmeta.post_id
    	   AND wpostmeta.meta_key = 'Partner_Level'
    	   AND wpostmeta.meta_value = '$cpLevel'
    	   AND wposts.post_status = 'publish'
    	   AND wposts.post_type = 'page'
    
    	   ORDER BY wposts.post_title ASC
    	 "; 
    
    	$pageposts = $wpdb->get_results($querydetails, OBJECT);
    
    	echo "<ul class=\"logobox clearfix\">";
    			foreach ( $pageposts as $page ) {
    					$option = '<li>';
    					$option .= '<a href="' . get_page_link( $page->ID ) . '">' . get_the_post_thumbnail( $page->ID ) . '</a>';
    					$option .= '</li>';
    					echo $option;
    			}
    	echo "</ul>";
    }
Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Get pages list with custom fields’ is closed to new replies.