• Resolved JF

    (@j_folkes)


    I created 2 custom fields on my posts (state and city). I created a new template that queries all the posts where meta_key=Georgia and everything, including pagination works great. The problem I am having is I need to sort these posts by a custom field called ‘city’ and I’m unsure how to do that. When I add orderby to the query then the pagination stops working. The results on the 2nd page are the same as the first page.

    $the_query = new WP_Query( '$args&meta_key=city&orderby=meta_value&order=ASC' );

    When I add the additional custom field ‘city’ to the $args, then I get no search results found.

    $args = array(
    	'paged'		=> $paged,
    	'posts_per_page' =>'20',
    	'meta_key'	=> 'state',
    	'meta_value'	=> 'Georgia',
    	'cat'		=> '624,613,625,626',
    	'meta_key'	=> 'city',
    	'orderby'  	=> 'meta_value',
    	'order'		=> 'ASC'
    );
    $the_query = new WP_Query( $args); ?>

    I’ve also tried adding:

    $city = get_post_meta( get_the_ID(), 'city', true );?>

    but if I add it before the loop it returns nothing when I echo $city and if I add it after the loop, it returns the city of the last post on my list and repeats that loop over and over and over, so I end up with thousands of posts on my page instead the 25 or so it should be.

    Can anyone steer me in the right direction of a tutorial on this? I am rather new to wordpress (other than plugins) and I’m quite proud of how far I’ve gotten but I just need this little bit more to finish the site and go live. I have been googling for days on this and the closest thing I’m finding with multiple custom fields is using meta_query with an AND/OR condition, but that’s not quite my situation or I’m just not seeing how to do it.

    Thanks in advance!

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

    (@j_folkes)

    Figured it out. The meta_query WAS the answer. Pasting the code in case it can help anyone else.

    My Code

    The pagination is a function. I wish I could remember the website I got it from. It was a tutorial and very helpful.
    Pagination code

Viewing 1 replies (of 1 total)
  • The topic ‘how to sort query by custom field NOT in my $args’ is closed to new replies.