0

So this is my Query. where category 17 is, I want this to by dynamic based on the page you're on. So I have a custom meta field called city_name but when I just put that in there. it obviously doesnt work. How do I get the 'cat' to show the custom meta name

// WP_Query arguments
$args = array (
'post_type'              => 'advertisement',
'category_name'          => 'city_name',
'posts_per_page'         => '2',
'order'                  => 'DESC',
'orderby'                => 'date',
 );

// The Query
$search_ads = new WP_Query( $args );

// The Loop
if ( $search_ads->have_posts() ) {
while ( $search_ads->have_posts() ) {
    $search_ads->the_post(); ?>

<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>

<?php }
} else {
// no posts found
}

EDIT:

I started doing this:

$key_1_value = get_post_meta( get_the_ID(), 'city_name',   true );

// WP_Query arguments
$args = array (
'post_type'              => 'advertisement',
'category_name'          => '$key_1_value',

doesn't seem to work. If I make category name "toronto" which is the "city_name" I am looking for, it works perfect. Just need that city_name to be drawn dynamically..

EDIT PROBLEM SOLVED.

ok so this turned out to be almost perfect. however, you have to reset the query, before drawing the value. Good thing it didn't take me 100 billion hours to figure out. Thanks India.

wp_reset_query

Add the reset right after the $key_value line before the arguements, then you can draw your custom meta field into the arguments.

  • If I understand you correctly, you want to get the category name or ID of the category page you are cuurently on, and then you need to feed that name/ID back into your query – Pieter Goosen Jun 7 '14 at 5:15
  • You should post your solution as an answer, then after 48 hours come back and accept your own answer – Pieter Goosen Jun 7 '14 at 6:43
0

Change this

'category_name' => '$key_1_value',

To this

'category_name' => $key_1_value,

You don't need '' around $key_1_value

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.