Support

Account

Home Forums ACF PRO Dynamic creation of category selector

Solving

Dynamic creation of category selector

  • Hello,

    what i need is to let my client select featured posts of categories in his site. For this I added a local field group in my plugin code.

    			array(
    				'key'               => 'field_6172b1e27f165',
    				'label'             => 'Featured cat posts',
    				'name'              => 'featured_cat_posts',
    				'type'              => 'post_object',
    				'instructions'      => '',
    				'required'          => 0,
    				'conditional_logic' => 0,
    				'wrapper'           => array(
    					'width' => '',
    					'class' => '',
    					'id'    => '',
    				),
    				'post_type'         => array(
    					0 => 'post',
    				),
    				'taxonomy'          => array(
    					0 => 'category:'.$slug,
    				),

    This is real magic.
    Testing with
    $slug = "clients";
    i’m able to select articles of may category “clients”.

    This dynamic code fails

    $catinfo = get_category( $tagID );
    $slug    = trim( $catinfo->slug );

    even if the array is the same

    'taxonomy'          => array(
    					0 => 'category:clients',
    				),

    Is there any wizard of oz function to get this working ?-))
    I trie almost everything to be sure that this respects the format.

    Cheers and thanks for all your ideas and helping
    Christian

  • Hi @cbilledatasolution-fr

    Do you need to select the category or posts within the category?

  • Hello @jarvis,

    the aim is to select/edit a category, and then select the featured posts of that category. Maybe there is another way to do this ?

    Cheers

  • Hmmm…

    I’ve done it that you can select the category (using the taxonomy field > categories).

    Then had a field to set the number of posts to return:

    <?php
    global $wp_query;
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    
    $post_category = get_field('post_category');
    $number_of_posts = get_field('number_of_posts');
    
    $args = array(
    	'post_type'			=> 'post',
    	'post_status'		=> 'publish',
    	'posts_per_page'	=> $number_of_posts,
    	'paged'				=> $paged,
    	'cat'		       	=> $post_category,
    	'fields'			=> 'ids'	
    );
    
    $wp_query = new WP_Query($args);
    if ($wp_query->have_posts()) :
    	while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    		<?php include( locate_template( 'includes/loop.php', false, false ) ); ?>
    	<?php endwhile; 
    endif; wp_reset_query(); 

    Not sure how I’d then select the posts themselves. My only thought would be adding a tickbox to the posts themselves called ‘featured post’, you then amend the query to return only the posts in that category that have been ticked.

    Seems a little long winded though 🙁

  • Hi,

    thanks. This is indeed some nice wp_query. I’m afraid that this will not do the job for me. If cannot solve this strange behavior with the category selector, wil try something else. Option page, repeater for categories and selection of posts.

    I have some great luck that i’m not too pressured by time.

    Cheers and have a nice weekend

  • No worries, have a good weekend and good luck!

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.