• Hey there! I have a request for a specific custom loop.

    1. I want to display only custom post types “success-stories” on a page
    2. I want to display only the posts in success stories with the custom field values $company1, $company2, $company3
    3. $company1, $company2, $company3 are defined in custom fields on the host page panel (pricing page for example).

    Here’s what I have so far :

    <?php 
    
    		// what companies to highlihght - that works I verfied
    		$company1 = sprintf("'%s'", get_field( 'company_highlight_1' ));
    		$company2 = sprintf("'%s'", get_field( 'company_highlight_2' ));
    		$company3 = sprintf("'%s'", get_field( 'company_highlight_3' ));
    
    		// args
    		$args = array(
    			'post_type'		=> 'success-story',
    			'meta_query' => array(
    				array(
              'key' => 'name_company',
              'value' => array($company1, $company2, $company3),
              'compare' => 'IN',
            )
        	)
    		);
    
    		$loop = new WP_Query( $args );
    
    		?>
    
    		<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    		<div class="men__test">
    			<h4><?php echo get_field('company_name', $post->ID); ?></h4>
    			<p><?php echo get_field('front_page_extract', $post->ID); ?></p>
    			<a class="men__hlink" href="story.html">Learn how <?php echo get_field('company_name', $post->ID); ?> uses XXXXX</a>
    		</div>
    
    		<?php endwhile; ?>
    		<?php endif; ?>

    But unfortunately it displays nothing.

    Any idea? Thanks a lot!

Viewing 1 replies (of 1 total)
  • Thread Starter gillesb

    (@gillesb)

    Got it

    <?php
    		// what companies to highlihght
    		$company1 = get_field( 'company_highlight_1' );
    		$company2 = get_field( 'company_highlight_2' );
    		$company3 = get_field( 'company_highlight_3' );
    
    		// args
    		$args = array(
    			'numberposts'	=> -1,
    			'post_type'	=> 'success-story',
    			'meta_query' => array(
    				'relation'	=> 'OR',
    	      array(
    					'key'	=> 'name_company',
    					'value'	=> $company1,
    					'compare'	=> 'LIKE'
    				),
    				array(
    					'key'	=> 'name_company',
    					'value'	=> $company2,
    					'compare'	=> 'LIKE'
    				),
    				array(
    					'key'	=> 'name_company',
    					'value'	=> $company3,
    					'compare'	=> 'LIKE'
    				)
        	)
    		);
    
    		$loop = new WP_Query( $args );
    		?>

    Thanks 🙂

Viewing 1 replies (of 1 total)
  • The topic ‘Custom Loop by Post Types AND Custom Meta Values’ is closed to new replies.