Support

Account

Forum Replies Created

  • Completely thanks to all your help! The answer was basically found by putting together most of your suggestions and identifying correct variables to work with. Even the last bit you pointed me in the right direction. Thanks again, very much!

  • OK… I finally got it. This is what the correct query should have been, combining ACF and CPT arguments:

    $relatedCat = get_field('service_related_industry');
         $args = array (
    		'posts_per_page' => 3,
                    'post_type' => 'case_study',
    		'tax_query' => array(
    			array(
    				'taxonomy' => 'cs_industry',
    				'terms' => $relatedCat,
    				'field' => 'term_id',
    			),
    		),
    		'tag_id' => '"'.$relatedCat.'"',
    		'orderby' => 'DESC',
          );
    
  • Your question has made me realise that the issue is probably not with the ACF part but the Custom Post Types taxonomy I was querying.

    get_field is correctly pulling the field ID. However, my $args was not querying for posts filtered by taxonomy. I’ve changed the code to:

      <?php $relatedCat = get_field('service_related_industry');
         $args = array (
    		'posts_per_page' => 3,
                    'post_type' => 'case_study',
    		'tag_id' => '"'.$relatedCat.'"',
    		'orderby' => 'DESC',
    

    It’s still not working properly, but at least now it isn’t pulling all the posts (it’s pulling exactly the 3 latest, while ignoring the ‘tag_id’ filter).

  • cs_industry is a taxonomy attached to the custom post type ‘case_study’. It does seem to save the values I tick for each post. Not sure if that answers your question?

  • First of all, thank you for your continued help. I really appreciate it.

    I tried the code you suggested, but haven’t yet solved either of the issues. I feel like we’re getting closer to finding the issue.

    The cs_industry is a taxonomy relating to the custom post type “case study”. And the variable $relatedCat does give a number (e.g. 1079).

    When I print_r($args), I get:
    Array ( [posts_per_page] => 3 [post_type] => case_study [meta_query] => Array ( [0] => Array ( [key] => cs_industry [value] => "1079" [compare] => LIKE ) ) [orderby] => DESC )

  • Sorry if I’m being thick, but the first $arg references a brand new ACF field that’s not referenced anywhere else, so I can’t imagine that anything outside of the code on this page could be influencing its variables.

    Is there a way to override settings from elsewhere?

  • But then that wouldn’t explain why the second query works properly. If there were code elsewhere that was changing the number of posts shown, surely it would affect both conditions – before and after the IF statement.

  • To clarify further, the whole code snippet is as follows:

      $relatedCat = get_field('service_related_industry');
         $args = array (
    		'posts_per_page' => 3,
                    'post_type' => 'case_study',
    		'meta_query' => array(
    			array(
            		'key' => 'cs_industry',
    				'value' => $relatedCat,
    				'compare' => 'LIKE',
    				)
    			),
    		'orderby' => 'DESC',
          );
    	 if (!$relatedCat) { 
    		 $relatedCat = get_field('service_related_category');
    		 unset ($args);
         	 $args = array (
            	'posts_per_page' => 3,
            	'cat' => $relatedCat,
            	'orderby' => 'DESC',
          	 );
    	 }
    

    If I comment out the first line, the IF statement works perfectly, and in fact it also adjusts the number of posts correctly. So, I reckon the problem is in the way the first field (service_related_industry) is being handled. But I can’t quite figure out what’s going wrong.

  • Thanks for helping, John. However, the nested array didn’t solve the issue. It’s still just showing the latest posts without filtering based on value.

    Also, there isn’t any other code on the page determining how many posts should be pulled, not that I can find for this query. Is there a way to make this one override any others?

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