Use more than one post meta inside the loop

I have custom meta st_date and end_date

I want to select the post with both of these conditions are true for :

Read More

1- end_date = “some date”

2- st_date = “some date”

3-Also I need it to select by title

4-Then used the result inside the WordPress loop.

if ( have_posts() ) : ?>
<?php get_template_part('loop'); ?> //etc..

Here is my draft code:

$st_date_s = 'something';
$end_date_s = 'something';
$pageposts = get_posts(
    array(
        'post_title'      => 'something'
        'post_type'      => 'something',
        'post_status'    => 'publish',
        'meta_query'     => array(
            array(
                array( 'key' => 'st_date', 'value' => $st_date_s, 'compare' => '=' ), 
                array( 'key' => 'end_date', 'value' => $end_date_s, 'compare' => '=' )
            )       
        ),  
    )
);
?>
<?php global $site_url;
if ( have_posts() ) : ?>
<?php get_template_part('loop'); ?>
<?php else : ?>//etc..

Also why ‘post_title’=> doesn’t work?

Related posts

Leave a Reply

1 comment

  1. You’re missing a relation argument, and have one-too-many array()s. Since you want both conditions to be true, you should use AND:

    ...
    
        'meta_query'     => array(
            'relation' => 'AND',
            array( 'key' => 'st_date', 'value' => $st_date_s, 'compare' => '=' ), 
            array( 'key' => 'end_date', 'value' => $end_date_s, 'compare' => '=' ),
        ),  
    
    ...
    

    Read more about custom field parameters in the docs.