• Resolved Sarah_Frantz

    (@sarah_frantz)


    Trying to display a custom field value from a custom post type at random on page load. My query works, but because each post has an empty value for the custom field, sometimes on page load the spot for the custom field value appears to be blank.. I want it to ONLY grab custom field values that actually has text in it… here is my quer

    $quotequery = new WP_Query( 'post_type=knowledgebase&meta_key=pull_quote&showposts=1&orderby=rand');
    if ( $quotequery->have_posts()) {
    	while ( $quotequery->have_posts() ) {
    		$quotequery->the_post();

    Thoughts on how I can limit this to only show posts that actually have a value in the pull_quote custom field?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    How did you add the custom fields? I don’t think it’s possible to add custom fields with an empty value in the edit posts screen.

    try it with this [untested]:

    $args = array(
    	'post_type' => 'knowledgebase',
    	'meta_query' => array(
    		array(
    			'key' => 'pull_quote',
    			'value' => '',
    			'compare' => '!='
    		)
    	)
    );
    $quotequery = new WP_Query( $args );

    http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters

    Thread Starter Sarah_Frantz

    (@sarah_frantz)

    I guess I forgot to mention I’m using Advanced Custom Fields, so the field is a custom field value, and it is always present…

    Thread Starter Sarah_Frantz

    (@sarah_frantz)

    I switched it to check for a yes or no and display based on that, which seems to be working now to only show articles with the quote, however it isn’t randomizing it..

    $args = array(
    	'post_type' => 'knowledgebase',
    	'orderby' => 'RAND',
    	'showposts' => 1,
    	'meta_query' => array(
    		array(
    			'key' => 'show_quote?',
    			'value' => 'Yes'
    		)
    	)
    );
    $quotequery = new WP_Query( $args );
    Thread Starter Sarah_Frantz

    (@sarah_frantz)

    RESOLVED. Moved orderby above post type and it works like a charm.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Hopefully a simple query question’ is closed to new replies.