Support

Account

Home Forums General Issues Insert Cat ID into Query

Solved

Insert Cat ID into Query

  • I’ve been banging my head on this all day! I’m sure it is more of a PHP problem than an AFC issue.

    I’m trying to insert a category id into a query and it just isn’t working out. Right now it just displays the category id on the page and isn’t being used by the query, it is simply straight up displaying the ID # but no posts. If I replace “the_sub_field(‘part_category’)” manually with a category ID, eg: 66, I get a list of posts, but right now the code simply displays the part category ID of 66. – I hope that is clear!

    For instance, the code:

    
    query_posts(array(
        'cat' => the_sub_field('part_category'), // get posts by category id
        'posts_per_page' => -1 // all posts
    ));
    ?>
    
    <?php while(have_posts()): the_post(); ?>
        <h1><?php the_title(); ?></h1>
    

    So again, the above spits out the value entered for the “part_category” field but doesn’t add the category ID to query all posts in that category. How else can I add “the_sub_field(‘part_category’)” to the array in order for this to work?

  • Hi @skasprick

    any ACF function beginning with “the_” echoes out the result. Replace it with “get_” and it will instead return the result.

    That’s why you’re seeing the ID echoed on the page but not applied to your query.

    So do this:

    
    query_posts(array(
        'cat' => get_sub_field('part_category'), // get posts by category id
        'posts_per_page' => -1 // all posts
    ));
    ?>
    

    and it should work better.

    I do feel the need to inform you that using query_posts is a nono in the WP developer community since quite some time. If you’re looking to alter a core query you should take a look at the pre_get_posts filter: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    and If you’re looking to create a whole new set of posts you should look at wp_query: codex.wordpress.org/Class_Reference/WP_Query

  • I think we’re closer, but no cigar 🙁 but I’ve also moved on to wp-query. I have it set up, but it’s still not pulling the category for the query. Here is my complete loop:

      <?php if( have_rows('parts_table') ): ?>
    
        <?php while( have_rows('parts_table') ): the_row(); ?>    
        
    
    <?php
    $catquery = new WP_Query( 'order=ASC&orderby=meta_value&meta_key=weight_(lbs.)&showposts=200&cat='.get_sub_field('part_category').'&post_type=sweeps' ); ?>
    <table>
    <tr><td>Part Number</td><td>Description</td><td>Weight (lbs.)</td>
    <?php while($catquery->have_posts()) : $catquery->the_post();
    ?>
    
                    <tr>
                        <td>
                        <?php the_field('part_number'); ?>
                        </td>
                        <td>
                        <?php the_field('description'); ?>
                        </td>
                        <td>
                        <?php the_field('weight_(lbs.)'); ?>
                        </td>            
                    </tr>
                
    
    <?php endwhile; ?>
          <?php wp_reset_query(); ?>
        </table> 
          <?php endwhile;?>
        <?php endif; ?>

    So there is my loop. If I manually enter a cat id it works, but it won’t pull my ACF field. Thanks so far!

  • Alright.

    I think your main issue lies in the meta_key value. I sincerely hope that’s not the actual meta field name. These should only contain small letters, 0-9 and possibly _ or – for separation.

    Have a look at this and see if there’s something needing change.. look at the comments I’ve put in.

    
    <?php
    $part_category = get_sub_field('part_category'); // This HAS to be a string value. Make sure that it's not an array or term object.
    $args = array(
    	'post_type' => 'sweeps',
    	'posts_per_page' => -1, //fetches ALL posts without limit
    	'order' => 'ASC',
    	'orderby' => 'meta_value',
    	'meta_key' => 'weight_lbs',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'category', // If you're using a custom taxonomy this needs to be changed
    			'terms' => array($part_category),
    			'field' => 'term_id'	
    		)
    		
    	}
    );
    $catquery = new WP_Query($args); 
    ?>
    
  • Good snippet! But unfortunately it’s the same situation in that if I manually enter the id of the category, replacing “array($part_category)”, it works, but as it is I don’t get any result from the code.

    I’m not sure, but I’m wondering if I’ve set up the custom field wrong, and after that, if there is something funky set up in CPT UI? For now I’ve attached a screen shot from ACF for the field.

    Thanks!

  • Sorry – my screen shot above was premature. It has some settings that were experiments and I forgot to change them back – you’ll notice I had the category returning a Term Object above – but as you’ll see in this new screen shot, I’m returning the Term ID and still get the same result of not being able to enter the category into the query through a custom field.

    Thanks!
    Scott

  • have you tried “term object” instad if “term ID” in the ACF field?

  • Hi @skasprick

    Hm okay. I think you might have two different issues here:

    1. Sometimes when changing the return value you need to resave the post/term/whatever for the changes to occur. If you initially had term object you might want to try resaving.
    2. I’m fairly certain that when using the checkbox layout the return wont just be an ID integer but an array. Try this:

    
    <?php
    $part_category = get_sub_field('part_category'); // This HAS to be a string value. Make sure that it's not an array or term object.
    $args = array(
    	'post_type' => 'sweeps',
    	'posts_per_page' => -1, //fetches ALL posts without limit
    	'order' => 'ASC',
    	'orderby' => 'meta_value',
    	'meta_key' => 'weight_lbs',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'category', // If you're using a custom taxonomy this needs to be changed
    			'terms' => array($part_category[0]),
    			'field' => 'term_id'	
    		)
    		
    	)
    );
    $catquery = new WP_Query($args); 
    ?>
    

    Or possibly:

    
    <?php
    $part_category = get_sub_field('part_category'); // This HAS to be a string value. Make sure that it's not an array or term object.
    $args = array(
    	'post_type' => 'sweeps',
    	'posts_per_page' => -1, //fetches ALL posts without limit
    	'order' => 'ASC',
    	'orderby' => 'meta_value',
    	'meta_key' => 'weight_lbs',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'category', // If you're using a custom taxonomy this needs to be changed
    			'terms' => array($part_category[0]->term_id),
    			'field' => 'term_id'	
    		)
    		
    	)
    );
    $catquery = new WP_Query($args); 
    ?>
    
  • Great!

    The below Solved my problem! Just one note, I had to change the last curly bracket to a bracket just above the Wp_Query. Not having a matching meta_key tripped me up for just a minute too, but temporarily eliminating the reordering proved the code is good! Thanks! This helps some of us continue to make a living! 🙂

    The winning code:

    <?php
    $part_category = get_sub_field('part_category'); // This HAS to be a string value. Make sure that it's not an array or term object.
    $args = array(
    	'post_type' => 'sweeps',
    	'posts_per_page' => -1, //fetches ALL posts without limit
    	'order' => 'ASC',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'category', // If you're using a custom taxonomy this needs to be changed
    			'terms' => array($part_category[0]),
    			'field' => 'term_id'	
    		)
    		
    	)
    );
    $catquery = new WP_Query($args); 
    ?>
  • Great!
    I’ve fixed the brackets in the post. Mistake from my part 🙂

    Best of luck in making a living!

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

The topic ‘Insert Cat ID into Query’ is closed to new replies.