Support

Account

Forum Replies Created

  • Hi @nineten!

    Check this: https://www.advancedcustomfields.com/resources/acf-fields-post_object-query/

    So your code should be:

    
    
    function my_post_object_query( $args, $field, $post_id ) {
    	
        // only show parent posts
        $args['post_parent'] = 0;
    
        // return
        return $args;
        
    }
    
    // filter for a specific field based on it's name
    add_filter('acf/fields/post_object/query/name=my_post_object', 'my_post_object_query', 10, 3);
    
    
  • Hi!

    When you create a taxonomy field you can select the return value. Select term object instead of term ID.

  • Hi DanielWinnard!

    Yes, you can use an option page (https://www.advancedcustomfields.com/add-ons/options-page/). Then in your pages you can do something like this:

    
    <?php
    
    // current page download links (your different set of download links)
    $download_links_to_show = get_field(‘download_links’);
    
    // if you dont have links on that page
    if(empty($download_links_to_show)) {
    
         // use common download links
         $download_links_to_show = get_field(‘common_download_links’, ‘option’);
    }
    
  • Yep, it’s not bad, you are removing all of your terms from that post. Maybe you should use wp_delete_object_term_relationships( $object_id, $taxonomies ); but your solution works too.

  • Hi nicoloaversa! Try this:

    
    <?php
    // get current author object
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
    $author_id = $curauth->ID;
    
    $user_facebook = get_field('user-facebook', 'user_'.$author_id); // see https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/
    
    if( $user_facebook ): ?>
    
    <h5><?php echo $user_facebook; ?></h5>
    
    <?php else: ?>
    // do something else
    <?php endif; ?>
    
  • Hi elliottc, maybe you could delete that meta field with delete_post_meta. You should pass the post ID.

    
    delete_post_meta($post_id, 'images');
    
  • Yep, that worked for me. What are the names of the custom post types? I’m asking you this because the maximum length of the custom post type slug is 20 characters.

    Send me an email to [email protected] if you want, I will try to help you

  • Ok, try this, I changed the location of the add_action hook and changed “movie” texts with $cpt_name

    
    if( have_rows('additional-post-type', 'option') ):
        function custom_post_type() {
            while ( have_rows('additional-post-type', 'option') ) : the_row();
         
            $cpt_name = get_sub_field('cpt-name');
            $cpt_slug = slugify($cpt_name);
    
            register_post_type( $cpt_slug , array(
                'label'               => __( $cpt_slug, 'twentythirteen' ),
                'description'         => __( $cpt_slug.' news and reviews', 'twentythirteen' ),
                'labels'              => array(
                        'name'                => _x( $cpt_name, 'Post Type General Name', 'twentythirteen' ),
                        'singular_name'       => _x( $cpt_name, 'Post Type Singular Name', 'twentythirteen' ),
                        'menu_name'           => __( $cpt_name, 'twentythirteen' ),
                        'parent_item_colon'   => __( 'Parent '.$cpt_name, 'twentythirteen' ),
                        'all_items'           => __( 'All '.$cpt_name, 'twentythirteen' ),
                        'view_item'           => __( 'View '.$cpt_name, 'twentythirteen' ),
                        'add_new_item'        => __( 'Add New '.$cpt_name, 'twentythirteen' ),
                        'add_new'             => __( 'Add New', 'twentythirteen' ),
                        'edit_item'           => __( 'Edit '.$cpt_name, 'twentythirteen' ),
                        'update_item'         => __( 'Update '.$cpt_name, 'twentythirteen' ),
                        'search_items'        => __( 'Search '.$cpt_name, 'twentythirteen' ),
                        'not_found'           => __( 'Not Found', 'twentythirteen' ),
                        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
                    ),
                'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
                'hierarchical'        => false,
                'public'              => true,
                'show_in_nav_menus'   => true,
                'show_in_admin_bar'   => true,
                'menu_position'       => 5,
                'can_export'          => true,
                'has_archive'         => true,
                'exclude_from_search' => false,
                'publicly_queryable'  => true,
                'capability_type'     => 'page',
                )
            );
         
            endwhile;
        }
        
        add_action( 'init', 'custom_post_type', 0 );
    endif;
    
  • Ok, try this, I changed the location of the add_action hook and “movie” texts with $cpt_name

    
    if( have_rows('additional-post-type', 'option') ):
        function custom_post_type() {
            while ( have_rows('additional-post-type', 'option') ) : the_row();
         
            $cpt_name = get_sub_field('cpt-name');
            $cpt_slug = slugify($cpt_name);
    
            register_post_type( $cpt_slug , array(
                'label'               => __( $cpt_slug, 'twentythirteen' ),
                'description'         => __( $cpt_slug.' news and reviews', 'twentythirteen' ),
                'labels'              => array(
                        'name'                => _x( $cpt_name, 'Post Type General Name', 'twentythirteen' ),
                        'singular_name'       => _x( $cpt_name, 'Post Type Singular Name', 'twentythirteen' ),
                        'menu_name'           => __( $cpt_name, 'twentythirteen' ),
                        'parent_item_colon'   => __( 'Parent '.$cpt_name, 'twentythirteen' ),
                        'all_items'           => __( 'All '.$cpt_name, 'twentythirteen' ),
                        'view_item'           => __( 'View '.$cpt_name, 'twentythirteen' ),
                        'add_new_item'        => __( 'Add New '.$cpt_name, 'twentythirteen' ),
                        'add_new'             => __( 'Add New', 'twentythirteen' ),
                        'edit_item'           => __( 'Edit '.$cpt_name, 'twentythirteen' ),
                        'update_item'         => __( 'Update '.$cpt_name, 'twentythirteen' ),
                        'search_items'        => __( 'Search '.$cpt_name, 'twentythirteen' ),
                        'not_found'           => __( 'Not Found', 'twentythirteen' ),
                        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
                    ),
                'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
                'hierarchical'        => false,
                'public'              => true,
                'show_in_nav_menus'   => true,
                'show_in_admin_bar'   => true,
                'menu_position'       => 5,
                'can_export'          => true,
                'has_archive'         => true,
                'exclude_from_search' => false,
                'publicly_queryable'  => true,
                'capability_type'     => 'page',
                )
            );
         
            endwhile;
        }
        
        add_action( 'init', 'custom_post_type', 0 );
    endif;
    
  • You’re welcome!

    I tried with your code and didn’t work for me too, so I did a few changes and now works for me.
    In this code I use a function called slugify (https://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string), to create the CPT slug.

    Tell me if it works for you

    
    if( have_rows('additional-post-type', 'option') ) {
    
        function register_post_types() {
    
           while ( have_rows('additional-post-type', 'option') ) { the_row();
    
                $cpt_name = get_sub_field('cpt-name');
                $cpt_slug = slugify($cpt_name);
    
                // new custom post type
                register_post_type(
                    $cpt_slug , array(
                        'public' => true,
                        'labels' => array(
                            'name' => __($cpt_name, 'theme'),
                            'singular_name' => __($cpt_name, 'theme'),
                        ),
                        'has_archive' => true,
                        'supports' => array(
                            'title',
                            'thumbnail',
                        ),
                    )
                );
            }
    
        }
    
        add_action( 'init', 'register_post_types' );
    }
    
  • Hi Damian P. Try using get_sub_field(‘cpt-name’) instead of the_sub_field(‘cpt-name’)

    
    <?php 
    if( have_rows('additional-post-type', 'option') ):
       while ( have_rows('additional-post-type', 'option') ) : the_row();
    
       function custom_post_type() {
            $labels = array(
                'name' => _x( get_sub_field('cpt-name'), 'Post Type General Name', 'twentythirteen' ),
                // others
            );
            $args = array(
                // options
            );
            register_post_type( 'movies', $args );
        }
        add_action( 'init', 'custom_post_type', 0 );
    
    endwhile;
    endif;
    
  • Hi ForbiddenChunk! You should use the post ID instead of the post object in the post_parent argument. Take a look at this, may helps you in the future https://gist.github.com/luetkemj/2023628

    
    <?php
    $post_object = get_field('second_subpage');
    
    if( $post_object ): 
    
    // get the post id from the post object
    $post_ID = $post_object->ID;
    
    // override $post
    $post = $post_object;
    setup_postdata( $post ); 
    ?>
        <!-- here -->
    	<?php
    	$args = array(
    	    'post_parent'    => $post_ID,
    	    'post_type'      => 'page',
    	    'posts_per_page' => -1,
    
    	    'order'          => 'ASC',
    	    'orderby'        => 'menu_order'
     	);
    
    	$parent = new WP_Query( $args );
    
    	if ( $parent->have_posts() ) : 
    	while ( $parent->have_posts() ) : $parent->the_post();
    	?>
    
    	<div class="page_link page_link--small count_five" style="background-image:url('<?php the_post_thumbnail_url(); ?>');">
    		<div class="page_link__title">
    			<h2><?php the_title(); ?></h2>
    			<?php the_field('preview'); ?>
    		</div>
    	</div>
    
    <?php endwhile; ?>
    <?php endif; wp_reset_postdata(); ?>
    
        <!-- end -->
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>
    
  • Hi saltnpixels, could you give us your code to help you?

  • Try this

    
    <?php 
        if( !empty($addons_included_ids) && is_array($addons_included_ids) && in_array($addon->ID, $addons_included_ids) ): ?>
    
  • Hi lepel

    You should add the post ID in the get_field function: https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/

    
    <?php 
    $post_ID = 5; // this is the ID of the page which contains the values selected
    $beschikbaar = get_field('beschikbaar', $post_ID);
    
  • Hi nwde, you tried something like this?

  • Cool, take a look at this and tell me if it works.

    
    <?php 
    // this field contains an array of addons IDs
    $addons_included_ids = get_field('add_ons_suites');
    
    $addons = get_posts(array(
      'post_type' => 'addons',
      'posts_per_page' => -1 
    ));
    
    ?>
    <?php if( $addons ): ?>
    
    <table class="table table-hover table-sm inclusions">
    	<thead>
    		<tr>
    			<th colspan="2" scope="colgroup" class="text-uppercase">Product Inclusion</th>
    		</tr>
    	</thead>
    
    	<tbody>
    
    	<?php foreach( $addons as $addon ): ?>
    
    		<tr>
    			<td>
    				<a href="<?php echo get_permalink( $addon->ID ); ?>">
    				<i class="fas fa-chevron-right mr-1"></i><?php echo get_the_title( $addon->ID ); ?></a>
    			</td>
    
    			<td>
    			<?php 
    			// check if the current addon ID exists in the $addons_included_IDS array
    			if( in_array($addon->ID, $addons_included_ids) ): ?>
    
    				<i class="fas fa-check"></i>
    
    			<?php else: ?>
    
    				<i class="fas fa-times"></i>
    
    			<?php endif; ?>
    			</td>
    		</tr>
    
    	<?php endforeach; ?>
    	<?php wp_reset_postdata(); ?>
    	
    	</tbody>
    
    </table>
    <?php endif; ?>
  • I asked you what returns because we need to know what we have (post object or post ID) to do the relations between the addon and the current post.

    So the add_ons_suites is a relationship which contains a single value of the post ID or multiple values of posts IDs?

    And another question, the add_ons_suites is a field from the cpt addon or the current post?

    This questions will help me to give you a better resolution

  • The principal difference is your code executes a query, wich if the $ids is empty the query with the argument “post__in” will be empty, so retrieves all of the testimonials posts, and then checks if you have $ids or not. So this does a call to the database and then checks for the $ids.

    In my code checks if you have $ids or not, if you don’t have $ids then don’t do the query (which means don’t do a call to the database)

    I hope this helps you.

  • Hi renee, what does the add_on_suites field return? An array of the post or the post ID?

  • Hi Nessler, sorry for the late reply.

    Tell me if it works for you

    
    <div id="meta-coauthor">
    	<span class='metacoauteur'>
    	<?php 
    		$values = get_field('coauteurs'); 
    		if($values) { 
    			foreach($values as $value)	{
    				$author_ID = $value['ID'];
    				$author_url = esc_url( get_author_posts_url($author_ID) );
    
    				echo ', <a href="'.$author_url.'">' . $value['display_name'] . '</a>';
    			}	
    		};
    	?>
    	</span>
    </div>
    
    
  • Sorry, I forgot to open and close the last php tag

    
    <?php 		
    // get only first 1 results
    $ids = get_field('testimonials_relationship', false, false);
    
    // check if have ids
    if($ids) {
    
    	$the_query = new WP_Query(array(
    		'post_type'      	=> 'testimonials',
    		'posts_per_page'	=> 1,
    		'post__in'			=> $ids,
    		'post_status'		=> 'any',
    		'orderby'        	=> 'rand',
    	));
    
    	?>
    
    	<?php if ( $the_query->have_posts() ) : ?>
    
    		<!-- pagination here -->
    
    		<!-- the loop -->
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    			<h2><?php the_title(); ?></h2>
    		<?php endwhile; ?>
    		<!-- end of the loop -->
    	    
    		<!-- pagination here -->
    
    		<?php wp_reset_postdata(); ?>
    
    	<?php else : ?>
    		<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
    	<?php endif; ?>
    
    <?php } ?>
    
    
  • You should check if you have testimonials ids

    
    <?php 		
    // get only first 1 results
    $ids = get_field('testimonials_relationship', false, false);
    
    // check if have ids
    if($ids) {
    
    	$the_query = new WP_Query(array(
    		'post_type'      	=> 'testimonials',
    		'posts_per_page'	=> 1,
    		'post__in'			=> $ids,
    		'post_status'		=> 'any',
    		'orderby'        	=> 'rand',
    	));
    
    	?>
    
    	<?php if ( $the_query->have_posts() ) : ?>
    
    		<!-- pagination here -->
    
    		<!-- the loop -->
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    			<h2><?php the_title(); ?></h2>
    		<?php endwhile; ?>
    		<!-- end of the loop -->
    	    
    		<!-- pagination here -->
    
    		<?php wp_reset_postdata(); ?>
    
    	<?php else : ?>
    		<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
    	<?php endif; ?>
    
    }
    
    
Viewing 25 posts - 1 through 25 (of 26 total)