Support

Account

Home Forums Front-end Issues True – false

Solved

True – false

  • Hi there,

    I have a simple catalog which show child pages of a parent page. But I want to display some pages of catalog on main page. So I created a checkbox “True / False” and want possibility to display desired catalog subpages on main if True is selected.

    I’ve come up with this code

    <?php
    if ( 'yes' == get_field('show-main') ):
    $mypages = get_page ( array( 'sort_column' => 'menu_order','sort_order' => 'ASC',  ) );
    $counter = 1;
    foreach( $mypages as $page ) {		
    $content = $page->post_content;
    if($counter % 4 == 0) {
    echo '<div class="row mb-30">';
    }
    ?>
    loop
    <?php if($counter % 4 == 0) {
      echo '</div>';
    }
    $counter++;
    }	
    ?>
    <?php else: ?>
    <?php endif; ?>

    But with no luck so far… Any suggestions how to make this work?

    Thanks

  • @redeclipse

    get_page has been deprecated… it is recommended to use get_post() in favour of it.

    With that said… would a custom WP_Query be a better choice in this scenario?

    If you agree… check out an example below of the arguments that you could pass to the WP_Query when using a True/False ACF field.

    Hopefully you can pick apart and make sense of it for your use case:

    $projects_args = array(
      'post_type' => 'projects',
      'post_status' => 'publish',
      'relation' => 'AND',
      'meta_query' => array(
        array(
          'key' => 'cc_project_complete',
          'value' => FALSE,
          'compare' => '='
        ),
        array(
          'key' => 'cc_project_customer',
          'value' => $customer_id,
          'compare' => '='
        )
      ),
      'fields' => 'id, title, cc_project_job_num, cc_project_load_ship_date, cc_project_asap, cc_project_po_received_date, cc_project_dept_planned_1, cc_project_dept_planned_2, cc_project_dept_planned_3, cc_project_dept_planned_4, cc_project_dept_planned_5, cc_project_dept_planned_6, cc_project_dept_planned_7, cc_project_dept_planned_8, cc_project_dept_planned_9, cc_project_dept_planned_10, cc_project_dept_planned_11, cc_project_dept_planned_12, cc_project_dept_planned_13, cc_project_dept_planned_14, cc_project_dept_planned_15, cc_project_urgency, cc_project_asap_lead_time, cc_project_rush_lead_time, cc_project_standard_lead_time, cc_project_complete, cc_project_customer'
    );
    

    ‘cc_project_complete’ is my True/False ACF field. Also… you can decide what fields to return using ‘fields’. And in this example… I made sure that 2 criteria were satisfied by using ‘relation’ => ‘AND’ with my ‘meta_query’

    Hope that steers you in the right direction.

  • Thanks for the reply! I’ve come up with this code but unfortunately it still doesn’t work, can you tell me what’s wrong here?

    <?php 
    
    $posts = get_posts(array(
        'cat' => $current,
        'post_type' => array( 'page' ),
    	'meta_query' => array(
    		array(
    			'key' => 'show_main',
    			'compare' => '==',
    			'value' => '1'
    		)
    		)
    		)
    );
    
    if( $posts ): ?>
    	
    		
    	<?php foreach( $posts as $post ): 
    		
    		setup_postdata( $post )
    		
    		?>
    loop
    	<?php endforeach; ?>
    	
    	
    	<?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
  • @redeclipse

    It seems to look good in terms of no missing/extra brackets/commas, but I do see one potential issue.

    First, I can only assume $current has the appropriate data. During testing, you can leave one element out at a time… or hard code otherwise dynamic values.

    But.. the issue I see…

    When I was building my query… I looked at the existing ACF examples and docs and when using (as suggested):

    array(
      'key' => 'show_main',
      'compare' => '==',
      'value' => '1'
    )

    .. it didn’t work for me.

    But when I changed it to what made more sense to my brain anyway… which was:

    array(
      'key' => 'show_main',
      'compare' => '=',
      'value' => TRUE
    )

    … then it worked!

    That quick change might do it for you. I am not sure if double equals matters… I didn’t see double equals in the WP docs so I used single (which is the default). Note: I do not wrap TRUE in quotes in my example.

    Also, my bad on steering you towards get_posts() – it seems to be just a matter of preference in most cases.

    I noticed that in your original post you were using get_page() (singular) and I hadn’t heard of it, so I looked it up and saw that it had been deprecated in favour of, as it turns out: get_post() (singular)

    Now… I know you can achieve near same results with get_pages() and get_posts() (both plural)… maybe, just maybe, get_pages() is more appropriate for you. I only say that because Pages are hierarchical by nature, and maybe that fact is important to you and your query. I don’t know… but… with that said… with get_page() falling away, perhaps get_pages() will as well. Time will tell.

    At any rate, if you decide to jump over to get_pages() you just have to modify your args slightly.

    Best of luck with your project!

  • Thanks for the help, changed few extra strings to make everything work as I want to. As for value ‘1’ or TRUE(both work fine) I misspelled and wrote show_main in the key instead of show-main, heh.

    In case if someone will need this

    <?php 
    
    $posts = get_posts(array(
        'cat' => $current,
        'post_type' => array( 'page' ),
    	'numberposts'       => -1,
    	'meta_query' => array(
    		array(
    			'key' => 'show-main',
    			'value' => '1',
    		)
    		)
    		)
    );
    
    if( $posts ): ?>
    		
    	<?php foreach( $posts as $post ): 
    		
    		setup_postdata( $post )
    		
    		?>
    loop
    
    	<?php endforeach; ?>
    	
    	<?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘True – false’ is closed to new replies.