• I have created a Gutenberg block using the Metabox block plugin. In this Gutenberg block, I used a custom query to display custom posts based on a custom taxonomy and create custom category pages. Here I want to display the number of posts in the title, similar to the category pages (https://nviewscareer.com/position/phd/). So I tried using “pre_get_document_title” and “wp_title” to change the page title (https://nviewscareer.com/positions/postdoc/). But not able to change. so just added as h2 between the content. Please have a look at the code below.
    Thanks in advance and really appreciate your time and effort to help me.

    function query_filter_block($attributes, $is_preview = true, $post_id = null){
        
        if ( empty( $attributes['data'] ) ) {
            return;
        }
    
        // Unique HTML ID if available.
        $id = 'filter-' . ( $attributes['id'] ?? '' );
        if ( ! empty( $attributes['anchor'] ) ) {
            $id = $attributes['anchor'];
        }
    
        // Custom CSS class name.
        $class = 'filter ' . ( $attributes['className'] ?? '' );
        if ( ! empty( $attributes['align'] ) ) {
            $class .= " align{$attributes['align']}";
        }
        //get the data
        $post_type = mb_get_block_field( 'post_type' );
        $no_posts = mb_get_block_field( 'no_posts' );
        $countries= mb_get_block_field( 'country' );
        $workplaces = mb_get_block_field( 'workplace' );
        $positions = mb_get_block_field( 'position' );
        $status = mb_get_block_field( 'status' );
        $type = mb_get_block_field( 'type' );
        $subjects = mb_get_block_field( 'subjects' );
       
       //$country_id = array();
       
       $tax_q1 = array(
            'relation' => 'AND', 
            array(
                'taxonomy' => 'current-status',
                'field' => 'slug',
                'terms' => 'ongoing',
            ),
        );
        $tax_q2= array();
        if( !empty( $countries ) ){
            $country_id = wp_list_pluck($countries,'term_id');
            $tax_q2 = array([
                'taxonomy' => 'country',
                'field' => 'term_id',
                'terms' => $country_id,
                //'operator' => 'IN'
                ]);
        }
        
        $tax_q = array_merge($tax_q1, $tax_q2);
        
        //global $wp_query;
        
        $args = array(
            'post_type' => $post_type, // Post type
            'post_status' => 'publish',
            'orderby' => 'modified',
            'order'   => 'DESC',
            'wp_grid_builder' => 'wpgb-content',
            'tax_query' => $tax_q,
            'posts_per_page' => $no_posts,
            'fields' => 'ids',
            'cache_results'  => false,
            'update_post_meta_cache' => false,
            'update_post_term_cache' => false,
            //'no_found_rows' => true,
        );
        
        $queryid = 'filter-' . ( $attributes['id'] ?? '' ) . '-query';
        $filter = $queryid;
        
        $filter = new WP_Query($args);
        
        if( $filter->have_posts() ){
           
            $total_jobs = 0;
            $date_now = date('d');
        	
            $total_jobs = $filter->found_posts;
            $title = get_the_title();
            $title = $title . ' - ' . $date;
    
            // trying to use query data to alter the post title
            function custom_query_title($title){
                 $title = $total_jobs;
                 return $title;
            }
            add_filter('pre_get_document_title', 'custom_query_title');
    
            // add_filter('wp_title', 'custom_query_title');
            //both option are helpless to set the title of the current page
                   
            ?>
            <div id="<?php echo $id; ?>" class="<?php echo $class; ?>" >
                <h2><?php echo $total_jobs . ' ' . $title; ?></h2>
                <ul class="wpgb-content nvs-fblock-list">
                <?php
                $i = 0;
                while( $filter->have_posts() ){
                    $filter->the_post();
                    $title = get_the_title();
                    $url = get_post_permalink();//$data[0];
                    $date = wp_date('D, d M Y (H:i T)', $deadline);
                    the_title();
                ?>
            </div>
        <?php    
        }    
        wp_reset_postdata();    
    }

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The filter “the_title” can be used to alter titles output by calling the_title(). However, if you’re making custom queries on a template, at the time the title is output, the custom query hasn’t yet run and you’ve no count to add to the title.

    The best option is to alter the main query (through “pre_get_posts” action) to get the results you want. Then the count of results will be available from the global $wp_query object. This will also make pagination much easier.

    The other solution would be to run the query before the title is output, saving the results for later use. Or make a separate count query beforehand, then do the full data query as usual.

    Thread Starter Prabakaran Shankar

    (@nviews)

    Thank you very much for your kind suggestions, I will try to check them and update them here.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘set title of the page using gutenburg block based custom query data’ is closed to new replies.