Need help with a custom query for Jobs & Experts

In jobs and experts, there is a line "setup_postdata(get_post($pro->id))". I would like this query to only retrieve posts that were posted TODAY...I use the php function "$today = getdate();" to get today's date but i'm not sure how to pass the information to get_post().

Or should I need to use WP_Query. If so, how can I use it with setup_postdata(get_post($pro->id)). Please help!

  • Ivan
    • Developer

    Hi Tahiti Sunshine !

    If you want to add your restriction to that place ( it applies for jbp-expert-archive-page - is Expert Archive Page ) - you could try adding the following snippet in a MU plugin ( more info about MU plugins is here )

    <?php
    function wpmu_jobs_plus_today_expert( $args ) {
    	$today = getdate();
    	$args[ 'year' ] = $today["year"];
    	$args[ 'monthnum' ] = $today["mon"];
    	$args[ 'day' ] = $today["mday"];
    	return $args;
    }
    add_filter( 'jbp_expert_search_params', 'wpmu_jobs_plus_today_expert' );

    Best,
    Ivan.

  • Tahiti Sunshine
    • Design Lord, Child of Thor

    Hi Ivan,

    Thank you for your response. I don't quite seem to be able to get it to work. I've added this code to functions.php:

    function wpmu_jobs_plus_today_expert( $args ) {?>
    <script>
    jQuery(document).ready(function($){
    $today = getdate();
    $args[ 'year' ] = $today["year"];
    $args[ 'monthnum' ] = $today["mon"];
    $args[ 'day' ] = $today["mday"];
    return $args;
    });
    </script>
    <?php }
    ---
    If I add the following line to functions.php, the search page in Jobs & Experts won't load:
    add_filter( 'jbp_expert_search_params', 'wpmu_jobs_plus_today_expert' );

    If I add that line of code to the search page, either before or after this line:
    setup_postdata(get_post($pro->id));
    then it doesn't seem to filter any results.

    Can you direct me where to add "add_filter( 'jbp_expert_search_params', 'wpmu_jobs_plus_today_expert' );" so that it will filter the search results?

    Thanks again!

  • Ivan
    • Developer

    Hi Tahiti Sunshine !

    Why did you change the wpmu_jobs_plus_today_expert function? I provided you another version of the function. Please, use the version which I provided. You can create {wordpress_folder}/wp-content/mu-plugins/wpmu_jobs_plus_today_expert.php file via FTP. It's called MU plugin.
    Or you can add the original snippet which I provided to the function.php.

    Best,
    Ivan.

  • Tahiti Sunshine
    • Design Lord, Child of Thor

    Hi Ivan,

    Yes, the function that you suggested works well across the entire site when I add it to functions.php. But I should clarify that I'd like to add the "add_filter..." directly into the main.php file in the .../expert-archive directory. That way the function is not applied across the entire site.

    When I place the "add_filter ..." term directly above or below the line "setup_postdata(get_post($pro->id));" in main.php then the function doesn't filter the results as it should.

    Can you advise as to how best to apply this function using "add_filter..." within main.php?

    Thanks again

  • Ivan
    • Developer

    Hi Tahiti Sunshine !

    I already suggested the best way - use MU plugin or functions.php of a Child theme.
    We don't recommend you to insert any code to any plugin because you will lose these changes after updating these plugins.

    That way the function is not applied across the entire site.

    That function applies only if jbp_expert_search_params filter is running.

    Best,
    Ivan.

  • Tahiti Sunshine
    • Design Lord, Child of Thor

    Hi Ivan,

    Thanks for your points. Unfortunately, I do need the function to be applied locally within a page. For example, within a specific class or id. Is there a way to add the filter only within a specific class or id?

    Could $this be used to solve this? For example, could I apply "add_filter( 'jbp_expert_search_params', array( $this, 'wpmu_jobs_plus_today_expert' ) );" somewhere in /expert-archive/main.php? If so, which line of the code would I apply it?

    I understand this addition to main.php will be lost when I update the plugin. That's ok with me, I would add the addition back into main.php after I update in the future.

  • Ivan
    • Developer

    Hi Tahiti Sunshine !

    Is there a way to add the filter only within a specific class or id?

    Do you mean html class or html id? If yes - it's not possible.

    If you use the shortcode on your custom page - you can try to modify the MU plugin in the following way:

    <?php
    function wpmu_jobs_plus_today_expert( $args ) {
    	global $post;
    
    	if ( !empty( $post->ID ) && 112 === $post->ID ) {
    		$today = getdate();
    		$args[ 'year' ] = $today["year"];
    		$args[ 'monthnum' ] = $today["mon"];
    		$args[ 'day' ] = $today["mday"];
    	}
    	return $args;
    }
    add_filter( 'jbp_expert_search_params', 'wpmu_jobs_plus_today_expert' );

    Note: 112 is ID of the page - you can see it on Edit page in Admin area.

    Best,
    Ivan.

  • Tahiti Sunshine
    • Design Lord, Child of Thor

    Hi Ivan,

    That suggestion technically works. But for unrelated reasons, it's not an option for me to add the function to functions.php and set a post ID.

    I'd like to add your original function to /expert-archive/main.php. I understand this will have to be added to main.php again each time I update the plugin - that is ok with me. I've tried adding the function to main.php but it seems to have no effect. Can you direct me how to correctly add the function to main.php so it will work properly (eg. which line of main.php to add the function)? Thanks

  • Ivan
    • Developer

    Hi Tahiti Sunshine !

    If you don't want to implement it in the right way - you can add the following code to wordpress_folder/wp-content/plugins/jobs-plus/app/controllers/frontend/je-expert-archive-shortcode-controller.php file via FTP after $args = apply_filters( 'jbp_expert_search_params', $args ); line (approximately 33rd line )

    $today = getdate();
    $args[ 'year' ] = $today["year"];
    $args[ 'monthnum' ] = $today["mon"];
    $args[ 'day' ] = $today["mday"];

    You don't need to edit the main.php file.

    Best,
    Ivan.

  • Tahiti Sunshine
    • Design Lord, Child of Thor

    Hi Ivan,

    Thanks for your note, great suggestion. For unrelated reasons, I have to add your original function to /expert-archive/main.php. I understand this will have to be added to main.php again each time I update the plugin - that is ok with me.

    Can you direct me how to correctly add the function to main.php so it will work properly (eg. which line of main.php to add the function)? Thanks again

  • Ivan
    • Developer

    Hi Tahiti Sunshine !

    If you want to edit only the main.php file - you can try to add the following code before the setup_postdata(get_post($pro->id)); line

    $date = !empty( $col['item']->wp_post->post_date ) ? $col['item']->wp_post->post_date : '';
    if( !$date || date( "Y-m-d", strtotime( $date ) ) !== date( "Y-m-d" )  ) {
    	continue;
    }

    Best,
    Ivan.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.