• I have a function that I would like to run on the back-end so that it doesn’t load a post that will be hidden. I’m confused as to get it working in pre_get_post.

    The code:
    <?php if ((get_field('status_type') == 'alert') && get_field('status_start_date') < date( 'U', current_time( 'timestamp', 0 ) ) && (get_field('status_end_date')=='')):?>

    I’ve looked online and couldn’t find anything close to a solution. This is what I’ve done so far:

    function alerts_opby( $qvars ){
        $qvars[] = 'opby_alerts';
        return $qvars;
    }
    add_filter( 'query_vars', 'alerts_opby' );
    function opby_query( $query ) {
        if (isset( $query->$qvars['opby_alerts'] )) {
            $query->set('tax_query', array(array('taxonomy' => 'post_format','field' => 'slug','terms' => array( 'post-format-status' ),'operator'=> 'IN'), 'orderby' =>'meta_value','meta_key', 'breaking_news'),
            'meta_query', array(
                array(         
                    'value'   => date("U"),
                    'compare' => '<',          
                    'type'    => 'NUMERIC'         
                ),
                array(         
                    'value'   => array(get_field('status_time_duration', $post-id) + get_the_time('U') > date( 'U', current_time( 'timestamp', 0 ) )),
                    'compare' => '<',          
                    'type'    => 'NUMERIC'         
                )),  
            $query->set('posts_per_page', -1));
        }
        return $query;
    }
    add_action( 'pre_get_posts', 'opby_query' );
Viewing 4 replies - 1 through 4 (of 4 total)
  • It’s difficult to tell what you are trying to do. It looks like you want to change the query, but it also looks like you are trying to use the value of the field in an action that is before the values are retrieved.

    Thread Starter openbayou

    (@openbayou)

    Is this better to understand?

    if (isset( $query->$qvars['opby_alerts'] )) {
            $query->set('tax_query', array(array('taxonomy' => 'post_format','field' => 'slug','terms' => array( 'post-format-status' ),'operator'=> 'IN'), 'orderby' =>'meta_value','meta_key', 'breaking_news'),
            'date_query', array(
                'before' => date( 'U' , array(get_field('status_time_duration') + get_the_time('U')) )
            ),
            $query->set('posts_per_page', -1));
        }
    • This reply was modified 5 years, 9 months ago by openbayou.
    • This reply was modified 5 years, 9 months ago by openbayou.

    Well no, I thought you wanted help with code. Why are you using code to ask a question?
    What is get_field?

    Moderator bcworkz

    (@bcworkz)

    Joy, more than likely get_field() is declared by Advanced Custom Fields plugin to get meta values it saved.

    openbayou, it looks like you have the right idea. The devil is in the details. Perhaps output the global $wp_query->request on the template to see the SQL formed for your query. It could indicate why you are not getting the results you expect.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Comparing values in pre_get_posts’ is closed to new replies.