• Resolved 838745

    I have 2.6 WP version. Trying to get posts from one category. Using code

    $args = array(
    				'post_type' => 'post',
    				'category' => $cat->cat_ID,
    				'offset' => 1,
    				'numberposts' => 2,
    				'post_status' => 'publish',
    				'orderby' => 'date',
    				'order' => 'DESC',
    				'meta_key' => 'video',
    				'meta_value' => 1);
    			        $posts = get_posts($args);
    				foreach($posts as $post)
    				{
    					setup_postdata($post);
    ...

    And getting all posts from $cat->cat_ID and child cats. Any ideas how to fix?

    query_posts is useless, since it havent support of meta_key&meta_value

Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    query_posts does indeed support meta_key and meta_value in 2.6. So does the WP_Query method.

    In fact, get_posts is just using WP_Query now. It’s no longer any different. So switch already.

    Thread Starter 838745

    Hm, how then i should use it?
    Such code doesnt work:

    query_posts('cat=1&metakey=video&meta_value=1&post_type=post&offset=1&numberposts=2&post_status=publish&orderdy=date&order=desc');
    				while(have_posts())
    				{
    					the_post();

    Anyways, i know how to fix it to make work for me.

    wp-includes/query.php
    line 1022:
    $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));

    Just comment it and you will get directly what you asked for.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    “metakey” is not the same as “meta_key”. Spelling is important here…

    Also, using category__and instead of cat might be more what you’re looking for. Better than hacking the core code.

    Thread Starter 838745

    well. Good idea, but still not work:

    query_posts('cat='.$cat->cat_ID.'&meta_key=video&metavalue=1&post_type=post&offset=1&showposts=2&post_status=publish&orderdy=date&order=desc');
    				while(have_posts())
    				{
    					the_post();

    Such code still give me cat and subcats posts.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Yes, because the “cat” parameter gives the children as well.

    And you misspelled “meta_value” this time.

    Did you read what I posted about using category__and instead? Might be worth looking closer at that.

    Also, the array thing you were doing earlier with get_posts will work just as well with query_posts too. No need to do all that string work.

    Thread Starter 838745

    Thank you man for your help.

    Well, meta_value just was misspelled here, it works now.
    well, such code gives me 2nd and 3rd record i belive:

    $args = array(
    				'post_type' => 'post',
    				'category' => $cat->cat_ID,
    				'offset' => 1,
    				'showposts' => 2,
    				'post_status' => 'publish',
    				'orderby' => 'date',
    				'order' => 'DESC',
    				'meta_key' => 'video',
    				'meta_value' => 1);
    
    				query_posts($args);

    Something else i can do?

    And even ‘category__and’ & ‘category__’ gives same (2nd and 3rd record).

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    ‘category’ => $cat->cat_ID,

    Did you even try 'category__and' => array($cat->cat_ID) at all?

    Thread Starter 838745

    Wow. It finally works. Big thanks for you!

    Does anyone know where the function for $args sits? I am trying to figure out how to get the images/attachments to order based on the order I give them in the admin.
    Currently they remain in the order they were uploaded even if I re-order them by dragging and dropping in the admin.

    I am wondering if it has something to do with ORDER BY in the mySQL call – and perhaps that call is done somewhere in a function related to $args?

    maybe it has something to do with this:
    attachment->menu_order

    About a minute ago it WAS showing the order of the images as they are in the admin. Now they just show up as 0 for every post. I can’t see anything that has changed since.

    Got it!

    I used a for loop and then checked against current Post Number using attachment->menu_order

    if ($currentAttachmentNum == $attachment->menu_order){

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘WP 2.6 get_posts’ is closed to new replies.