WP_Query example



    add_filter( 'excerpt_more', $filter_latest_posts_excerpt_more );

    if ( isset( $attributes['categories'] ) ) {
        $args['category__in'] = array_column( $attributes['categories'], 'id' );
    }
    if ( isset( $attributes['selectedAuthor'] ) ) {
        $args['author'] = $attributes['selectedAuthor'];
    }

    $query        = new WP_Query();
    $recent_posts = $query->query( $args );

    if ( isset( $attributes['displayFeaturedImage'] ) && $attributes['displayFeaturedImage'] ) {
        update_post_thumbnail_cache( $query );
    }

    $list_items_markup = '';

    foreach ( $recent_posts as $post ) {
        $post_link = esc_url( get_permalink( $post ) );
        $title     = get_the_title( $post );

        


    /** * Filters the query arguments used for the Recent Posts widget. * * @since 4.2.0 * * @param array $query_args The arguments passed to WP_Query to produce the list of posts. */
    $query_args = apply_filters( 'dashboard_recent_posts_query_args', $query_args );

    $posts = new WP_Query( $query_args );

    if ( $posts->have_posts() ) {

        echo '<div id="' . $args['id'] . '" class="activity-block">';

        echo '<h3>' . $args['title'] . '</h3>';

        echo '<ul>';

        $today    = current_time( 'Y-m-d' );
        $tomorrow = current_datetime()->modify( '+1 day' )->format( 'Y-m-d' );
        

function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
    $object_id     = (int) $object_id;
    $menu_item_ids = array();

    $query      = new WP_Query();
    $menu_items = $query->query(
        array(
            'meta_key'       => '_menu_item_object_id',
            'meta_value'     => $object_id,
            'post_status'    => 'any',
            'post_type'      => 'nav_menu_item',
            'posts_per_page' => -1,
        )
    );
    foreach ( (array) $menu_items as $menu_item ) {
        if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
            
$args = array(
        'post_parent'         => $post->ID,
        'fields'              => 'ids',
        'post_type'           => 'revision',
        'post_status'         => 'inherit',
        'order'               => 'DESC',
        'orderby'             => 'date ID',
        'posts_per_page'      => 1,
        'ignore_sticky_posts' => true,
    );

    $revision_query = new WP_Query();
    $revisions      = $revision_query->query( $args );

    if ( ! $revisions ) {
        return array(
            'latest_id' => 0,
            'count'     => 0,
        );
    }

    return array(
        'latest_id' => $revisions[0],
        


        if ( isset( $_REQUEST['order'] ) && in_array( strtoupper( $_REQUEST['order'] ), array( 'ASC', 'DESC' ), true ) ) {
            $args['order'] = strtoupper( $_REQUEST['order'] );
        }

        if ( ! empty( $_REQUEST['filter-status'] ) ) {
            $filter_status       = isset( $_REQUEST['filter-status'] ) ? sanitize_text_field( $_REQUEST['filter-status'] ) : '';
            $args['post_status'] = $filter_status;
        }

        $requests_query = new WP_Query( $args );
        $requests       = $requests_query->posts;

        foreach ( $requests as $request ) {
            $this->items[] = wp_get_user_request( $request->ID );
        }

        $this->items = array_filter( $this->items );

        $this->set_pagination_args(
            array(
                'total_items' => $requests_query->found_posts,
                

        add_filter( 'next_posts_link_attributes', $filter_link_attributes );
        // Take into account if we have set a bigger `max page`         // than what the query has.         global $wp_query;
        if ( $max_page > $wp_query->max_num_pages ) {
            $max_page = $wp_query->max_num_pages;
        }
        $content = get_next_posts_link( $label$max_page );
        remove_filter( 'next_posts_link_attributes', $filter_link_attributes );
    } elseif ( ! $max_page || $max_page > $page ) {
        $custom_query           = new WP_Query( build_query_vars_from_query_block( $block$page ) );
        $custom_query_max_pages = (int) $custom_query->max_num_pages;
        if ( $custom_query_max_pages && $custom_query_max_pages !== $page ) {
            $content = sprintf(
                '<a href="%1$s" %2$s>%3$s</a>',
                esc_url( add_query_arg( $page_key$page + 1 ) ),
                $wrapper_attributes,
                $label
            );
        }
        wp_reset_postdata(); // Restore original Post Data.     }
    

do_action( 'sanitize_comment_cookies' );

/** * WordPress Query object * * @global WP_Query $wp_the_query WordPress Query object. * @since 2.0.0 */
$GLOBALS['wp_the_query'] = new WP_Query();

/** * Holds the reference to @see $wp_the_query * Use this global for WordPress queries * * @global WP_Query $wp_query WordPress Query object. * @since 1.5.0 */
$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];

/** * Holds the WordPress Rewrite object for creating pretty URLs * * @global WP_Rewrite $wp_rewrite WordPress rewrite component. * @since 1.5.0 */
return new WP_Error( 'invalid_action', __( 'Invalid action name.' ) );
    }

    if ( ! in_array( $status, array( 'pending', 'confirmed' ), true ) ) {
        return new WP_Error( 'invalid_status', __( 'Invalid request status.' ) );
    }

    $user    = get_user_by( 'email', $email_address );
    $user_id = $user && ! is_wp_error( $user ) ? $user->ID : 0;

    // Check for duplicates.     $requests_query = new WP_Query(
        array(
            'post_type'     => 'user_request',
            'post_name__in' => array( $action_name ), // Action name stored in post_name column.             'title'         => $email_address,        // Email address stored in post_title column.             'post_status'   => array(
                'request-pending',
                'request-confirmed',
            ),
            'fields'        => 'ids',
        )
    );

    
$template_part_id = null;
    $content          = null;
    $area             = WP_TEMPLATE_PART_AREA_UNCATEGORIZED;

    if (
        isset( $attributes['slug'] ) &&
        isset( $attributes['theme'] ) &&
        get_stylesheet() === $attributes['theme']
    ) {
        $template_part_id    = $attributes['theme'] . '//' . $attributes['slug'];
        $template_part_query = new WP_Query(
            array(
                'post_type'           => 'wp_template_part',
                'post_status'         => 'publish',
                'post_name__in'       => array( $attributes['slug'] ),
                'tax_query'           => array(
                    array(
                        'taxonomy' => 'wp_theme',
                        'field'    => 'name',
                        'terms'    => $attributes['theme'],
                    ),
                ),
                
'posts_per_page' => 1,
        'no_found_rows'  => true,
        'post__not_in'   => array( $post_id ),
        'tax_query'      => array(
            array(
                'taxonomy' => 'wp_theme',
                'field'    => 'name',
                'terms'    => $theme,
            ),
        ),
    );
    $check_query      = new WP_Query( $check_query_args );
    $posts            = $check_query->posts;

    if ( count( $posts ) > 0 ) {
        $suffix = 2;
        do {
            $query_args                  = $check_query_args;
            $alt_post_name               = _truncate_post_slug( $override_slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
            $query_args['post_name__in'] = array( $alt_post_name );
            $query                       = new WP_Query( $query_args );
            $suffix++;
        } while ( count( $query->posts ) > 0 );
        
$query['offset'] = $args['pagenum'] > 1 ? $query['posts_per_page'] * ( $args['pagenum'] - 1 ) : 0;

        if ( isset( $args['s'] ) ) {
            $query['s'] = $args['s'];
        }

        $posts = array();

        // Prepend list of posts with nav_menus_created_posts search results on first page.         $nav_menus_created_posts_setting = $this->manager->get_setting( 'nav_menus_created_posts' );
        if ( 1 === $args['pagenum'] && $nav_menus_created_posts_setting && count( $nav_menus_created_posts_setting->value() ) > 0 ) {
            $stub_post_query = new WP_Query(
                array_merge(
                    $query,
                    array(
                        'post_status'    => 'auto-draft',
                        'post__in'       => $nav_menus_created_posts_setting->value(),
                        'posts_per_page' => -1,
                    )
                )
            );
            $posts           = array_merge( $posts$stub_post_query->posts );
        }

        

    public function find_oembed_post_id( $cache_key ) {
        $cache_group    = 'oembed_cache_post';
        $oembed_post_id = wp_cache_get( $cache_key$cache_group );

        if ( $oembed_post_id && 'oembed_cache' === get_post_type( $oembed_post_id ) ) {
            return $oembed_post_id;
        }

        $oembed_post_query = new WP_Query(
            array(
                'post_type'              => 'oembed_cache',
                'post_status'            => 'publish',
                'name'                   => $cache_key,
                'posts_per_page'         => 1,
                'no_found_rows'          => true,
                'cache_results'          => true,
                'update_post_meta_cache' => false,
                'update_post_term_cache' => false,
                'lazy_load_term_meta'    => false,
            )
        );

        $query = apply_filters( 'wp_link_query_args', $query );

        // Do main query.         $get_posts = new WP_Query();
        $posts     = $get_posts->query( $query );

        // Build results.         $results = array();
        foreach ( $posts as $post ) {
            if ( 'post' === $post->post_type ) {
                $info = mysql2date( __( 'Y/m/d' )$post->post_date );
            } else {
                $info = $pts[ $post->post_type ]->labels->singular_name;
            }

            
/** * Filters the arguments passed to WP_Query during an Ajax * call for querying attachments. * * @since 3.7.0 * * @see WP_Query::parse_query() * * @param array $query An array of query variables. */
    $query             = apply_filters( 'ajax_query_attachments_args', $query );
    $attachments_query = new WP_Query( $query );
    update_post_parent_caches( $attachments_query->posts );

    $posts       = array_map( 'wp_prepare_attachment_for_js', $attachments_query->posts );
    $posts       = array_filter( $posts );
    $total_posts = $attachments_query->found_posts;

    if ( $total_posts < 1 ) {
        // Out-of-bounds, run the query again without LIMIT for total count.         unset( $query['paged'] );

        $count_query = new WP_Query();
        
$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
    $page     = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];

    // Override the custom query with the global query if needed.     $use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
    if ( $use_global_query ) {
        global $wp_query;
        $query = $wp_query;
    } else {
        $query_args = build_query_vars_from_query_block( $block$page );
        $query      = new WP_Query( $query_args );
    }

    if ( $query->have_posts() ) {
        return '';
    }

    if ( ! $use_global_query ) {
        wp_reset_postdata();
    }

    $classes            = ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) ? 'has-link-color' : '';
    
Home | Imprint | This part of the site doesn't use cookies.