• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

ACF fields on archive page are blank if I use pre_get_posts – Why

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › ACF fields on archive page are blank if I use pre_get_posts – Why

This topic is: not resolved
  • This topic has 1 reply, 2 voices, and was last updated 6 years, 2 months ago by Victor Font.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • July 6, 2017 at 10:57 am #208782
    mmjaeger
    Member

    Hello
    I've a custom archive page showing some ACF field values - if I add the following piece of code to the functions.php file, all the ACF fields on the archive page are blank - nothing is shown on the archive page - I'd like to show only posts on that archive page where the status field is set to 'active' - what am I missing.
    thank you in advance for you input.

    add_action( 'pre_get_posts', 'themeprefix_pre_get_posts' );
    function themeprefix_pre_get_posts( $query ) {
    	
    	if ( is_admin() )
    		return $query;
    	
    	// Show only active job openings
    	if ( is_post_type_archive ( 'job' ) ) {
    		$query->set( 'meta_key',   'status' );
    		$query->set( 'meta_value', 'active' );
    	}
    	
    	return $query;
    }
    July 6, 2017 at 12:44 pm #208787
    Victor Font
    Moderator

    pre_get_posts is a little different from other hooks. With pre_get_posts, the $query object is passed to your function by reference. You do not need to return a value. Any changes you make to the object from inside your function are made to the original immediately.

    Also meta queries are handled a little differently from the main query object. The main query has an array called meta_query. You have to append your criteria to the meta_auery array.

    I haven't tested this, but the approach I would use is:

    add_action( 'pre_get_posts', 'themeprefix_pre_get_posts' );
    function themeprefix_pre_get_posts( $query ) {
    	
    	if ( is_admin() ) {
    		return;
    	}
    	
    	// Show only active job openings
    	if ( is_post_type_archive ( 'job' ) ) {
    		$meta_query = $query->get('meta_query');
    		$meta_query['status'] = array(
    	        	'key' => 'status',
    	        	'value' => "yes",
    	        	'compare' => '='
    	    	);
    		$meta_query['active'] = array(
    	        	'key' => 'active',
    	        	'value' => "yes",
    	        	'compare' => '='
    	    	);
    		$query->set( 'meta_query', $meta_query );
    	}
    }

    You'll have to match up the key and value to the actual values stored in the database, but this should at least point you in the right direction.


    Regards,

    Victor
    https://victorfont.com/
    Call us toll free: 844-VIC-FONT (842-3668)
    Have you requested your free website audit yet?

  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble