Support

Account

Home Forums Add-ons Options Page Query posts by options page custom field

Helping

Query posts by options page custom field

  • Hi,

    I am having troubles querying a custom post type by a custom field in an options page. Heres my set up:

    Custom post type = cars
    An options page under cars using:

    if( function_exists('acf_add_options_sub_page') )
    {
        acf_add_options_sub_page(array(
            'title' => 'Comp Options',
    		'menu_title' 	=> 'Comp Options',
            'parent' => 'edit.php?post_type=cars',
            'capability' => 'manage_options'
        ));
    }

    On the options page I have a field type “User”. This is set to show only registered users with the role Author. So on the Cars options page you can choose from about 10 Authors. Each Author has a front end profile page. If they have been added to Cars on the options page then I want the Cars entries to show.

    If… the field type User is added to the cars custom post type, and not the options page I can do this:

    <?php 
    $args = array(
    'post_type' => 'cars',	
    'posts_per_page' => -1,	// Displays ALL post
    'metaquery' => array(
    	array(
    	'key' => 'judges',
    	'value' => '"'.$user_ID.'"',
    	'compare' => 'LIKE'
    	)
    	));
    	// get results
    	$the_query = new WP_Query( $args );
    	?>
                    
    <?php if( $the_query->have_posts() ): ?>	
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    <?php the_field('comp_data'); ?>
                   
    <?php endwhile; ?>	
    <?php endif; ?>
    <?php wp_reset_query(); ?>

    And it works. So I thought I could do something like this:

    <?php 
    $ids = get_field('judges', 'option');
    $args = array(
    'post_type' => 'cars',	
    'posts_per_page' => -1,	// Displays ALL post
    'metaquery' => array(
    	 array(
    	'key' => $ids,
    	'value' => '"'.$user_ID.'"',
    	'compare' => 'LIKE'
    	 )
    ));
    // get results
    $the_query = new WP_Query( $args );
    ?>           
    <?php if( $the_query->have_posts() ): ?>	
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    <?php the_field('comp_data'); ?>
    <?php endwhile; ?>	
    <?php endif; ?>
    <?php wp_reset_query(); ?>

    But that just doesnt work. Can anyone explain how to query posts based on a custom field in an options page?

    Thanks

    Rob

  • If I get what you’re trying to do correctly, I think that you’ll need to have the user field both the options page and the user page.

    Set the user select field on the options page to return ID. This will return an array of user ID values.

    Then change your meta query to

    
        'meta_query' => array(
            array(
                'key' => '',
                'value' => $ids,
                'compare' => 'IN'
            ),
        ),
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.