Need help with WP Query

I need a help with a query. If I have two custom fields called _sfm_wpcf-ads-profession and _sfm_wpcf-job-profession. How can I get those posts when the values of these two fields are same?

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Menno ,

    In order to "read" the url parameter we have to utilize the $_GET array. As an example on the snippet that follows let's say that we go to test-domain.com/mypage/?_sfm_wpcf-job-profession=Developer .

    We will read the "_sfm_wpcf-job-profession" from $_GET and then run a customized query with get_posts ( https://developer.wordpress.org/reference/functions/get_posts/ ) so WordPress can return any post that contains that value in the _sfm_wpcf-ads-profession field.

    Of course you will have to adjust the code to your liking and to return/show any information from any post. Don't forget to change the "post_type" as well if it's a custom post type :slight_smile: .

    $url_arg = sanitize_text_field( $_GET['_sfm_wpcf-job-profession'] );
    
    $args = array(
    	'post_type'  => 'post',
    	'meta_key'   => '_sfm_wpcf-ads-profession',
    	'meta_value' => $url_arg,
    );
    
    $posts_array = get_posts( $args );
    
    foreach ( $posts_array as $post ) {
    	echo '<pre>';
    	var_dump( $post );
    	echo '</pre>';
    }

    The code above would return as anything that has "Developer" as an _sfm_wpcf-ads-profession .

    I hope this helps and tell me if you need any further help!

    Regards,
    Konstantinos

  • Menno
    • Flash Drive

    Hi Konstantinos Xenos

    Thanks for your response.

    I have this now:

    function lifejobs_ads() { 
    
    $key_profession = sanitize_text_field( $_GET['_sfm_wpcf-job-profession'] );
    $metafield_profession = types_render_field('wpcf-ad-test');
    
    $args = array(
    	'post_type'  => 'ad',
    	'posts_per_page' => '5',
    	'meta_key'   => $metafield_profession,
    	'meta_value' => $key_profession
    );
    
    // Custom query.
    $query = new WP_Query( $args );
    
    // Check that we have query results.
    if ( $query->have_posts() ) {
    
        // Start looping over the query results.
        while ( $query->have_posts() ) {
    
            $query->the_post();
    
    		?>
    
            <div style="float: left; width: 100%;">
    	        <?php the_title(); ?>
            </div>
    		<?php
        }
    
    }
    
    // Restore original post data.
    wp_reset_postdata();
    }
    add_shortcode( 'lifejobsads', 'lifejobs_ads' );
    
    This works! It gives 2 ads here with the custom field ad-test: https://www.lifejobs.io/jobs/?_sfm_wpcf-job-profession=Developer
    
    Is there a way to extend this with more? I extended it with:

    'meta_query' => array(
    'relation' => 'OR',
    array(
    'key' => 'key1',
    'value' => 'value1',
    'compare' => '='
    ),

    array(
    'key' => 'key1',
    'value' => 'value2',
    'compare' => '='
    )
    )

    But then it shows all results.

    Thanks,
    Menno

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Menno ,

    I'm not sure exactly how you extended your code but this should be working as an example:

    $url_arg     = sanitize_text_field( $_GET['_sfm_wpcf-job-profession'] );
    $url_arg_two = 'Sysadmin';
    
    $args = array(
    	'post_type'  => 'post',
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key'   => '_sfm_wpcf-ads-profession',
    			'value' => $url_arg,
    		),
    		array(
    			'key'   => '_sfm_wpcf-ads-profession',
    			'value' => $url_arg_two,
    		),
    	),
    );
    
    $posts_array = get_posts( $args );
    
    foreach ( $posts_array as $post ) {
    	echo '<pre>';
    	var_dump( $post );
    	echo '</pre>';
    }

    The above would returns any posts that have either Developer ( from url ) or Sysadmin set to their _sfm_wpcf-ads-profession .

    Note that the "single" meta_key and meta_value are removed from the original array, also the "=" compare isn't needed as that's the default :slight_smile: .

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.