Home Forums Meteorite Meteorite Facts

This topic is: resolved

Tagged: 

This topic contains 15 replies, has 2 voices, and was last updated by  Terra Themes 3 months ago.

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #992

    JuanMZC
    Participant

    Can I use variables for values rather than using integer numbers in Meteorite Facts widget as values are frequently changing? https://drive.google.com/open?id=19v_VfAA-hC1htc-ijofaJY2mg-qbgaxg

    #993

    Terra Themes
    Keymaster

    Hi,

    if you choose to not animate the values, you can put special characters or text in it. See an example on the demo page.
    Is that what you mean with variables?

    #994

    JuanMZC
    Participant

    No, I didn’t mean that. What I meant was variables like Custom Fields, for example, pa_loan-numbers, tt-project-detail-desc-one, $loan-size, etc,.. Some of those variables are global counters with numbers (they are changing every 1 hour based on, for example, number of projects, loan sizes,..), so I want to display those numbers in Meteorite Facts widget. Please, check this screenshot: https://drive.google.com/open?id=1mvnSWHF5I2_EBg-Jw_c1fWE2qcBMAl_w

    #995

    Terra Themes
    Keymaster

    Since your variable that stores the value exists only “somewhere”, the widget has no access to it. You can read more about the variable scope on php.net.

    However when you modify the widget file directly (or copy the file and make your own widget out of it in an extra plugin to safe to update) you may can get access to that variable.

    You can count the posts of the post type project with something like this. You just have to take a look which ID your category has.

    $args = array(
      'cat' => 4,
      'post_type' => 'projects'
    );
    $projects_query = new WP_Query( $args );
    echo $projects_query->found_posts;

    To get the sum of the you could maybe use something like this:

    $projects = new WP_Query( array(
      'no_found_rows'      => true,
      'post_status'        => 'publish',
      'post_type'          => 'projects',
      'posts_per_page'     => -1,
      'project-category'   => YOUR-CATEGORY-ID-HERE
    ) );
    
    $sumOfValues = 0;
    
    if ( $projects->have_posts() ) :
      while ( $projects->have_posts() ) :
        $detail_desc_one = get_post_meta( $post->ID, 'tt-project-detail-desc-one', true );
        $sumOfValues += $detail_desc_one;
      endwhile;
      wp_reset_postdata();
    endif;

    I haven’t tested this and it may need some modifications, but I can imagine that this is how you can achieve something like this. Please understand that I cannot test this further so you might want to hire a freelancer if you cannot do this on your own.

    #996

    JuanMZC
    Participant

    Thanks so much for your help. I understand it. I’ll try and let you know how it goes.

    #997

    Terra Themes
    Keymaster

    That’s great. You’re welcome. I’ll mark the topic as resolved.

    #999

    JuanMZC
    Participant

    Hi there,

    I’m trying to do this by myself. I think with a little more of help from your side I’ll get it.

    I’ve modified your code following your instructions. It looks fine, so, it should work.

    I guess I should include the code in this file /public_html/wp-content/plugins/meteorite-extensions/inc/widgets/widget-facts.php?

    My question is where should I include the code in that file? and How should I integrate these facts in the admin widget backend https://we.tl/t-GkP7hphJQv ?

    I need 4 facts for this page https://alternativedebt.com.au/borrower/ (red row):
    1. Numbers of Lenders

    $args = array(
      'post_type' => 'product'
    );
    $projects_query = new WP_Query( $args );
    echo $projects_query->found_posts;

    2. Completed Loans ($m)

    $projects = new WP_Query( array(
      'no_found_rows'      => true,
      'post_status'        => 'publish',
      'post_type'          => 'projects',
      'posts_per_page'     => -1,
      'project-category'   => 19
    ) );
    
    $sumOfValues = 0;
    
    if ( $projects->have_posts() ) :
      while ( $projects->have_posts() ) :
        $detail_desc_one = get_post_meta( $post->ID, 'tt-project-detail-desc-one', true );
        $sumOfValues += $detail_desc_one;
      endwhile;
      wp_reset_postdata();
    endif;

    3. Live Loans (value $m)

    $projects = new WP_Query( array(
      'no_found_rows'      => true,
      'post_status'        => 'publish',
      'post_type'          => 'projects',
      'posts_per_page'     => -1,
      'project-category'   => 20
    ) );
    
    $sumOfValues = 0;
    
    if ( $projects->have_posts() ) :
      while ( $projects->have_posts() ) :
        $detail_desc_one = get_post_meta( $post->ID, 'tt-project-detail-desc-one', true );
        $sumOfValues += $detail_desc_one;
      endwhile;
      wp_reset_postdata();
    endif;

    4. Live Transactions

    $args = array(
      'cat' => 20,
      'post_type' => 'projects'
    );
    $projects_query = new WP_Query( $args );
    echo $projects_query->found_posts;

    Thanks so much. I really understand this is a lot help from your side.

    #1000

    Terra Themes
    Keymaster

    Hi Juan,

    since you’re using the widget in multiple areas you can’t just output your numbers as the widget does normally. You would either have to create a copy of the widget or a checkbox (for example), that changes the output to your numbers.

    Here are some instructions to create a copy of the widget. Normally this should be done in a child theme or new plugin since if the code is modified directly the plugin cannot be updated without losing the changes.

    1. Copy the widget and name it for example widget-borrower.php.

    2. In line 8 change Meteorite_Facts to ADS_Borrower which stands for your company name.

    3. In line 11 change meteorite_facts_widget to ads_borrower_widget and change the description. In line 12 change the name and in line 13 do the same as in 11.

    The form and update function can stay the same.
    However you could also remove the fields for the values to not confuse anybody working with the widget. These fields to remove would be fact_one_max, fact_two_max, fact_three_max and fact_four_max and everything where these variables are used.

    4. In the widget function find echo $args['before_widget'];. Before that paste in your code. Since the code is used underneath the variables need some different names. Here is what it could look like:

    $product_query = new WP_Query( array(
      'post_type' => 'product'
    ) );
    $numberOfLenders = $product_query->found_posts;
    
    $completedLoans = new WP_Query( array(
      'no_found_rows'      => true,
      'post_status'        => 'publish',
      'post_type'          => 'projects',
      'posts_per_page'     => -1,
      'project-category'   => 19
    ) );
    
    $completedLoansValue = 0;
    
    if ( $completedLoans->have_posts() ) :
      while ( $completedLoans->have_posts() ) :
        $detail_desc_one = get_post_meta( $post->ID, 'tt-project-detail-desc-one', true );
        $completedLoansValue += $detail_desc_one;
      endwhile;
      wp_reset_postdata();
    endif;
    
    $liveLoans = new WP_Query( array(
      'no_found_rows'      => true,
      'post_status'        => 'publish',
      'post_type'          => 'projects',
      'posts_per_page'     => -1,
      'project-category'   => 20
    ) );
    
    $liveLoansValue = 0;
    
    if ( $liveLoans->have_posts() ) :
      while ( $liveLoans->have_posts() ) :
        $detail_desc_one = get_post_meta( $post->ID, 'tt-project-detail-desc-one', true );
        $liveLoansValue += $detail_desc_one;
      endwhile;
      wp_reset_postdata();
    endif;
    
    $projects_query = new WP_Query( array(
      'cat' => 20,
      'post_type' => 'projects'
    ) );
    $numberOfProjects = $projects_query->found_posts;

    5. Underneath echo $args['before_widget']; is the widget output. Use your variables where fact_one_max etc. stands. When using my code from above your varaibles are:

    $numberOfLenders (fact_one_max),
    $completedLoansValue (fact_two_max),
    $liveLoansValue (fact_three_max) and
    $numberOfProjects (fact_four_max).

    You have to put them into the data-to for the count animation and into the echo.

    Let me know if you need some further assistance.

    #1001

    Terra Themes
    Keymaster

    I forgot to mention how to load the widget. Normally this should be done in a child theme or new plugin since if the code is modified directly the plugin cannot be updated without losing the changes.

    1. Open meteorite-extensions/inc/widgets/loader.php and put borrower into the array.

    2. In meteorite-extensions/inc/page-builder.php add your widget class ADS_Borrower to the array of widgets.

    Now it should be available in the page builder.

    #1003

    JuanMZC
    Participant

    Thanks so much for you help.

    I’ve followed your steps however I can’t see the new widget in the page builder. Find the modify files and screenshot at the following link: https://we.tl/t-LT4Igc0mf4
    Please, could you have a look at them? It seems something it’s missing.

    I’ve changed the name of the widget to ADS Facts Borrower.
    I’m doing it on our demo site https://demo.alternativedebt.com.au

    Thanks in advance.

    #1004

    Terra Themes
    Keymaster

    As far as I can see this looks like it should.

    I forgot another file to edit. Please register your widget in meteorite-extensions/meteorite-extensions.php.

    There is a function called meteorite_extensions_widgets_init() which has to be modified too.

    #1005

    JuanMZC
    Participant

    After doing that, the widget is coming up 🙂

    Now, the issue is the project category as this code doesn’t work (it displays 0):

    $projects_query = new WP_Query( array(
    	'cat' => 20,
    	'post_type' => 'projects'
    	) );
    	$numberOfProjects = $projects_query->found_posts;

    However, this code displays fine the total number of projects:

    $projects_query = new WP_Query( array(
    	'post_type' => 'projects'
    	) );
    	$numberOfProjects = $projects_query->found_posts;

    Do you know why?

    On the other hand, I guess this line $liveLoansValue += $detail_desc_one; won’t work as tt-project-detail-desc-one has values such as “$200,000”. So, it’s not a number. In this case, I should add a new line like $liveLoansValue = str_replace('$', '', $liveLoansValue); Am I correct?

    #1006

    Terra Themes
    Keymaster

    After doing that, the widget is coming up 🙂

    That’s great!

    Now, the issue is the project category as this code doesn’t work (it displays 0):

    I changed the query a bit and tried it out, this one should show the correct number of posts.

    $project_query = new WP_Query( array(
      'no_found_rows' => true,
      'post_status' => 'publish',
      'post_type' => 'projects',
      'posts' => -1,
      'include' => 'YOUR-CAT-SLUG-HERE',
    ) );
    $numberOfProjects = count( $project_query->query );
    
    echo $numberOfProjects;

    On the other hand, I guess this line $liveLoansValue += $detail_desc_one; won’t work as tt-project-detail-desc-one has values such as “$200,000”. So, it’s not a number. In this case, I should add a new line like $liveLoansValue = str_replace(‘$’, ”, $liveLoansValue); Am I correct?

    You’re correct. If the values aren’t only numbers, the handling of the numbers has to be done manually.

    • This reply was modified 3 months ago by  Terra Themes. Reason: added working query
    #1008

    Terra Themes
    Keymaster

    The query mentioned in the latest edit of the post above should work now.

    #1009

    JuanMZC
    Participant

    I’ve made some changes in your code and it’s working fine. Thanks so much for your help.

    The below is the final code. It may be useful for someone.

    		$product_query = new WP_Query( array(
    		  'post_type' => 'product'
    		) );
    		$numberOfLenders = $product_query->found_posts;
    
    		
    		$completedLoans = new WP_Query( array(
    			'post_type' => 'projects',
    			'post_status'   => 'publish',
    			'posts_per_page' => -1,
    			'tax_query' => array(
    			array(
    				'taxonomy' => 'project-category',
    				'field'    => 'slug',
    				'terms'    => 'completed',
    			)
    			)
    		) );
    
    		$completedLoansValue = 0;
    
    		if ( $completedLoans->have_posts() ) : 
    		  while ( $completedLoans->have_posts() ) : $completedLoans->the_post();
    			$detail_desc_one = get_post_meta( get_the_ID(), 'tt-project-detail-desc-one', true );
    			$detail_desc_one = str_replace('$', '', $detail_desc_one);
    			$detail_desc_one = str_replace(',', '', $detail_desc_one);
    			$completedLoansValue += $detail_desc_one;
    		  endwhile;
    		  $completedLoansValue = substr($completedLoansValue, 0, 3);
    		  wp_reset_postdata();
    		endif;
    
    		
    		$liveLoans = new WP_Query( array(
    			'post_type' => 'projects',
    			'post_status'   => 'publish',
    			'posts_per_page' => -1,
    			'tax_query' => array(
    			array(
    				'taxonomy' => 'project-category',
    				'field'    => 'slug',
    				'terms'    => 'current',
    			)
    			)
    		) );
    
    		$liveLoansValue = 0;
    
    		if ( $liveLoans->have_posts() ) :
    		  while ( $liveLoans->have_posts() ) : $liveLoans->the_post();
    			$detail_desc_one = get_post_meta( get_the_ID(), 'tt-project-detail-desc-one', true );
    			$detail_desc_one = str_replace('$', '', $detail_desc_one);
    			$detail_desc_one = str_replace(',', '', $detail_desc_one);
    			$liveLoansValue += $detail_desc_one;
    		  endwhile;
    		  $liveLoansValue = substr($liveLoansValue, 0, 3);
    		  wp_reset_postdata();
    		endif;
    		
    		
    		$projects_query = new WP_Query( array(
    			'post_type' => 'projects',
    			'post_status'   => 'publish',
    			'posts_per_page' => -1,
    			'tax_query' => array(
    			array(
    				'taxonomy' => 'project-category',
    				'field'    => 'slug',
    				'terms'    => 'current',
    			)
    			)
    		) );
    		$numberOfProjects = $projects_query->found_posts;
Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic.