• Resolved jmgchengfreemight

    (@jmgchengfreemight)


    Good Day,

    i’m using the wp_query as new WP_Query( array( ‘category_name’ => ‘cat 1’, ‘posts_per_page’ => -1, ‘post_status’ => ‘publish’ ) ); and its returning the results properly

    although in this specific query. I only want to fetch the wp_posts.ID, wp_posts.post_data, and wp_posts.post_title
    These 3 columns are only what I want from my query.
    Is this possible to do in pre_get_posts hook? The only samples I see is how you can edit the where clause but not the select columns. And also let say I’m not allowed to use $wpdb->get_results custom sql.

    thanks

Viewing 1 replies (of 1 total)
  • Thread Starter jmgchengfreemight

    (@jmgchengfreemight)

    done solving my problem with reference of:
    http://www.danielauener.com/extend-wp_query-with-custom-parameters/

    function.php

    function archive_dd_pre_get( $query )
    {
    	if ( isset($query->query_vars['limit_column']) && $query->query_vars['limit_column'] == 'idtitledatestatustype' )
    	{
    		$query->limit_column = $query->query_vars['limit_column'];
    	}
    }
    add_action( 'pre_get_posts', 'archive_dd_pre_get' );
    function archive_dd_filter( $fields, &$wp_query )
    {
        global $wpdb;
    	if( $wp_query->query_vars['limit_column'] == 'idtitledatestatustype' ) {
    		$fields = "$wpdb->posts.post_date";
        }
        return $fields;
    }
    add_filter('posts_fields', 'archive_dd_filter', 10, 2);

    somewhere.php
    $o_query_result = new WP_Query( array( 'category_name' => 'cat 1', 'posts_per_page' => -1, 'post_status' => 'publish', 'limit_column' => 'idtitledatestatustype' ) );

Viewing 1 replies (of 1 total)

The topic ‘how to modify select columns returned by wp_query?’ is closed to new replies.