0

I am trying to get posts of custom post type 'job_listing' on bases of two custom fields i.e 1) _job_location 2) _company_name

this is working fine

but I also want to display the posts first that are from the given company i.e if the posts belong to XYZ company then they should be displayed first and then the rest of posts from the given location

$related_args = array(
    'post_type' => 'job_listing',
    'orderby'   =>  $company_name,
    'meta_key'  => '_company_name',
    'posts_per_page' => 6,
    'post_status' => 'publish',
    'post__not_in' => array( $post->ID ),
    'meta_query' => array(    

                        'relation' => 'OR',
                        array(
                              'key'   => '_job_location', 
                              'value' => $job_location,
                              ),
                        array(
                              'key'   => '_company_name', 
                              'value' => $company_name,
                              ),      

                    ),

    );

My sorting logic is wrong and I can seem to find out the solution .. so please help me out

1 Answer 1

0

"'orderby' => $company_name," should be "'orderby' => 'meta_value',"

If the value of _company_name is numeric - it should be meta_value_num.

Note that 'orderby' can only have a predefined value and ordering does it alphabetical

See the possible values at the WP_Query orderby parameter: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

The ordering parameter says: Order alphabetical by the values of meta_key - _company_name in your case. That is not what your question says.

If you only want posts from a specific company in first place you have to do 2 loops.

One with the $company_name as you did and one with excluding $company_name

First loops args:

$related_args_company = array(
    'post_type' => 'job_listing',
    'posts_per_page' => 6,
    'post_status' => 'publish',
    'post__not_in' => array( $post->ID ),
    'meta_query' => array(    
                        'relation' => 'AND',
                        array(
                              'key'   => '_job_location', 
                              'value' => $job_location,
                              ),
                        array(
                              'key'   => '_company_name', 
                              'value' => $company_name,
                              ),      

                    ),

    );

Second loops args:

$related_args = array(
    'post_type' => 'job_listing',
    'posts_per_page' => 6,
    'post_status' => 'publish',
    'post__not_in' => array( $post->ID ),
    'meta_query' => array(    
                        'relation' => 'AND',
                        array(
                              'key'   => '_job_location', 
                              'value' => $job_location,
                              ),
                        array(
                              'key' => '_company_name',
                              'value' => $company_name,
                              'compare' => 'NOT LIKE'
                        ),

                    ),

    );

kind regards, tom

7
  • hi Tom thanks for the inputs.. _company_name is a string Commented Feb 20, 2018 at 20:53
  • i edited now to fit more to your question. note that 'orderby' can only have a predifined value and ordering does it alphabeticaly Commented Feb 20, 2018 at 20:55
  • but now you have removed the orderby .. plus why the two loops Commented Feb 20, 2018 at 20:57
  • because: the ordering parameter says: order alphabetical by the values of 'meta_key' - thats not what your question says so if you have a company name "ABC" it will always be displayed before posts with company name "CBA" if you want a specific company in first place, its the only way. Commented Feb 20, 2018 at 21:00
  • 1
    hmm I was already doing like that .. with three loops .. one for company , second with AND (company && location ) , then OR (company || location ) ... all these above withh if else but I guess you are suggesting to merge the two queries given by you and then display the result Commented Feb 20, 2018 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.