• Hello!

    While working here I got to need to use the job-manager. It’s excellent, but when I deployed all the jobs, 2600, the problem started: “Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)”.

    After a look around the plugin code I found the problem: it stores ALL the posts in a variable ($jobman_shortcode_all_jobs) just to use a count method in some places to check the amount of posts available. Instead of storing so much data with no use, I changed it to retrieve just the number of posts using a custom query. So things are working cleaner, faster and using almost no memory compared to what it used to.

    To apply this idea I changed the var $jobman_shortcode_all_jobs to $jobman_shortcode_jobs_number in all the files of the plugin, removed all the count functions applied to it in all files and changed this piece of code in “frontend-jobs.php”:

    $jobs = get_posts( $args );
    
    	$args['posts_per_page'] = '';
    	$args['offset'] = '';
    	$args['numberposts'] = -1;
    	$jobman_shortcode_all_jobs = get_posts( $args );

    to:

    $jobs = get_posts( $args );
    
    	$query = "SELECT te.name FROM {$wpdb->prefix}posts po";
    	$query .= " LEFT JOIN {$wpdb->prefix}term_relationships re ON re.object_id = po.id";
    	$query .= " LEFT JOIN {$wpdb->prefix}term_taxonomy ta ON re.term_taxonomy_id = ta.term_taxonomy_id";
    	$query .= " LEFT JOIN {$wpdb->prefix}terms te ON te.term_id = ta.term_id";
    	$query .= " WHERE ta.taxonomy = 'jobman_category'";
    	$query .= " AND po.post_type='jobman_job'";
    	if( 'all' != $cat )
    		$query .= " AND te.name='".$args['jcat']."'";
    
    	$jobman_shortcode_jobs_number = $wpdb->query( $query );

    Oh! You have to add the wordpress global var $wpdb to the beginning of the function in the file “frontend-jobs.php” or things will not work. I’ll be like this:

    function jobman_display_jobs_list( $cat ) {
    	global $jobman_shortcode_jobs, $jobman_shortcode_jobs_number, $jobman_shortcode_category, $jobman_shortcodes, $jobman_field_shortcodes, $wp_query, $wpdb;

    I hope this will be helpful.
    ??

    https://www.ads-software.com/extend/plugins/job-manager/

Viewing 8 replies - 1 through 8 (of 8 total)
  • T Klein

    (@tklein87gmailcom)

    Does this fix the memory exhaustion in the back-end applications page also?

    Thread Starter kalkehcoisa

    (@kalkehcoisa)

    Not. It just fixes the front-end problem, it’s needed to fix it to use pagination in the admin page. Here it takes a long time to open the admin page but works fine.
    Anyway, it’s something I’ll take a look when I finish the higher priority tasks I have to do right now. Because sooner or later, it’ll become a problem for me as well.

    Thread Starter kalkehcoisa

    (@kalkehcoisa)

    It took a while to finish everything. Had to work on other projects at same time as well. What haven’t helped. However. Here is the fix I have made to insert pagination in the back-end. It lowers a lot the memory consumption. ^^

    Well, all the editing has been made in the file job-manager/admin-jobs.php.

    First, place this function at its beginning:

    function jobman_pagination($page=0, $pages)
    {
    	$out = '<script type="text/javascript" language="JavaScript">
    	jQuery(document).ready(function()
    	{
    		jQuery("#jobman_admin_pagination").change(function()
    		{
    			if( jQuery(this).val() != "" )
    				location.href = jQuery(this).val();
    		});
    	});
    	</script>';
    
    	$url = 'https://www.redepsi.com.br/port/wp-admin/admin.php?page=jobman-list-jobs&jobman-offset=';
    	$teste = true;
    	$out .= '<select id="jobman_admin_pagination" style="width:100px; float:right;">';
    	for ( $pagei=1; $pagei<=$pages; $pagei++ )
    	{
    		if( $teste && $page < 1 )
    		{
    			$teste = false;
    			$select = ' selected="selected"';
    		}
    		else
    			if( $page == $pagei )
    				$select = ' selected="selected"';
    			else
    				$select = '';
    		$out .= '<option value="' . $url . $pagei . '"'.$select.'>' . $pagei . '</option>';
    	}
    	$out .= '</select>';
    	$out .= '<label for="jobman_admin_pagination" style="float:right; margin-right: 10px; padding-top: 4px;">Page</label>';
    	return $out;
    }

    So, change this line: $jobs = get_posts( ‘post_type=jobman_job&numberposts=-1&post_status=publish,draft,future’ ); by this block of code:

    if( isset($_GET['jobman-offset']) )
    		$offset = $_GET['jobman-offset'];
    	else
    		$offset = 0;
    	$limit = 50;
    	$myquery = wp_count_posts("jobman_job");
    	$num_posts = $myquery->publish;
    
    	$pages = $num_posts / $limit + (($num_posts % $limit) > 0);
    	echo jobman_pagination( $offset, $pages );
    	$jobs = get_posts( 'post_type=jobman_job&numberposts='.$limit.'&post_status=publish,draft,future&offset='.$offset );

    Done. It’ll enable the pagination in your job manager admin showing 50 results per page. ??

    It worked

    You are a Freaking GENIUS

    Thanks a lot man

    One more favor

    Can you do some thing like this for applications page too

    This is an excellent addition to the code – easily added too.

    T Klein

    (@tklein87gmailcom)

    That is awesome kalkehcoisa. This helps so much, your time is much appreciated.

    I would remove your website address the $url line towards the top and tell people to put their own address there.

    Now like archiseek said… we need something like this on the applications page now to solve that memory exhausted issue. Would it be hard to port this code to work on that page?

    do you have solution to fix pagination problem on listing jobs after wp 3.4?

    toughride

    (@toughride)

    Hi kalkehcoisa,

    I wonder if you could help me please. If I want to add your code into the job manager plugin installed in MULTI SITE environment, what should I do?

    I only want to add pagination to the application list in the admin. Looking at your code, $url variable would be an issue in MS network I guess.

    Please help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Fixed problem of memory exhaustion’ is closed to new replies.