WordPress.org

Forums

[resolved] meta_query does not filter at all (10 posts)

  1. nr2012
    Member
    Posted 2 years ago #

    Hi there

    I'm trying to filter my pages by two specific page templates since an hour and I cannot figure out why it's not working.

    Here's my Code:

    $args = array(
    	'meta_query' => array(
    		'relation' => 'AND', //Optional, defaults to AND
    				array(
    					'key' => '_wp_page_template',
    					'value' => array ('page-frontpage.php', 'page-frontpage2.php' ),
    					 'compare' => 'IN'
    					)
    				),
    	'sort_column' => 'post_date',
    	'sort_order' => 'ASC',
    );
    $frontpages = get_pages($args);

    This should in my opinion filter all my pages for the ones with my two different frontpage-templates (1 and 2).
    I'm not quite sure aboute the 'compare' thing, but I also tried others. The thing is, it dos not have an effect at all.

    I also tried a different approach, where I repeat my meta_key.
    Like this:

    $args = array(
    'meta_query' => array(
    	'relation' => 'OR',
    		array(
    			'key' => '_wp_page_template',
    			'value' => 'page-frontpage.php',
    			'compare' => '=',
    			),
    		array(
    			'key'=>'_wp_page_template',
    			'value'=> 'page-frontpage2.php',
    			'compare' => '=',
    			)
    ),
    'sort_column' => 'post_date',
    'sort_order' => 'ASC',
    );
    $frontpages = get_pages($args);

    But this did not have an effect either.
    I would really be glad if somebody could help me out with this, because I obviously do not see the mistake I'm making...

    Thanks already for the help

    cheers

  2. esmi
    Forum Moderator
    Posted 2 years ago #

  3. nr2012
    Member
    Posted 2 years ago #

    Thanks for your answer, esmi!
    I mainly had a look at this:
    http://codex.wordpress.org/Class_Reference/WP_Meta_Query

    searched the forum several times and
    now had a look at your link.
    there I read this

    'meta_query' => array(
           array(
               'key' => 'age',
               'value' => array(3, 4),
               'compare' => 'IN',
           )
       )

    The only difference I spot is that here the values of the array (3 and 4) are written without quotation marks. I tried that before and tried it now again.
    No change. The filtering does not work.

    I really have no clue, what's going wrong here...

  4. nr2012
    Member
    Posted 2 years ago #

    Look at this post I found in the internet.

    http://www.wordpress.stackexchange.com/questions/60142/get-pages-not-accepting-my-query

    This guy seems just to have the exact same problem...
    Anyone knows that issue?

  5. keesiemeijer
    moderator
    Posted 2 years ago #

    Have you tried it with get_posts() and the argument 'post_type' => 'page'

    $args = array(
    	'post_type' => 'page',
    	'sort_column' => 'post_date',
    	'sort_order' => 'ASC',
    	'meta_query' => array(
    		array(
    			'key' => '_wp_page_template',
    			'value' => array ('page-frontpage.php', 'page-frontpage2.php' ),
    			'compare' => 'IN'
    		)
    	),
    );
    $frontpages = get_posts( $args );

    http://codex.wordpress.org/Template_Tags/get_posts

  6. nr2012
    Member
    Posted 2 years ago #

    I tried to use get_posts() and with the post_type = page.
    But then I just get 4 or 5 pages out of around 18, which should be displayed.
    I really, really have no idea why there are differences between my pages.

    But thanks for your help, keesiemeijer.
    I reached my goal now by inserting this code fragment, I found on the internet and adapted for my purposes:

    $querydetails = "
       SELECT wposts.*
       FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
       WHERE wposts.ID = wpostmeta.post_id
       AND wpostmeta.meta_key = '_wp_page_template'
       AND wpostmeta.meta_value = 'page-frontpage2.php'
       AND wposts.post_status = 'publish'
       AND wposts.post_type = 'page'
    
       OR wposts.ID = wpostmeta.post_id
       AND wpostmeta.meta_key = '_wp_page_template'
       AND wpostmeta.meta_value = 'page-frontpage.php'
       AND wposts.post_status = 'publish'
       AND wposts.post_type = 'page'
    
       ORDER BY wposts.post_date ASC
     "; 
    
     $frontpages = $wpdb->get_results($querydetails, OBJECT);

    I don't understand the code entirely but it's working and I'm happy ;).

    Anyway; if somebody knows more about the reason why the wordpress-built-in meta_query stuff does not work – i'd still be interested!

  7. keesiemeijer
    moderator
    Posted 2 years ago #

    No problem. I'm glad you found a solution.

    Maybe using 'posts_per_page' => -1, with the get_posts arguments would get you all the posts you wanted.

  8. nr2012
    Member
    Posted 2 years ago #

    THIS IS INDEED WORKING!

    $args = array(
    	'meta_query' => array(
    	       array(
    	           'key' => '_wp_page_template',
    	           'value' => array('page-frontpage.php', 'page-frontpage2.php'),
    	           'compare' => 'IN',
    	       )
    		),
    
    		'sort_column' => 'post_date',
    		'sort_order' => 'ASC',
    		'posts_per_page' => -1,
    		'post_type' => 'page'
    	);
     $frontpages = get_posts($args);

    I was curious if that would work, and yes it did!
    Could you explain to me, what effect the adding of that single line had?

  9. keesiemeijer
    moderator
    Posted 2 years ago #

    I think get_posts uses the "Blog pages show at most" number in wp-admin > Settings > Reading, to retrieve the amount of posts. With posts_per_page set to -1 it gets all the posts matching the criteria.
    http://codex.wordpress.org/Function_Reference/WP_Query#Pagination_Parameters

  10. nr2012
    Member
    Posted 2 years ago #

    Ah, sure thing! Logic and resolved!
    (Well, why 'get_pages' does not work, still remains a mistery...)
    Thanks a lot.

Topic Closed

This topic has been closed to new replies.

About this Topic