Two Queries with WP_Query append second one to end with Pagination - wordpress-theming

What I am trying to do is create two queries for properties. One will retrieve the regular results based on a normal query. The second query will retrieve properties closely related to the first query. I am able to run both queries and retrieve all results with posts_per_page set to unlimited and no pagination. The problem when adding pagination is that both loops run and display posts on each page.
The page would have 3 from the first loop followed by 3 from the second loop.
I have tried to merge the two queries into one and show them but the same results happen. 3 and 3.
I am thinking that I need to append somehow to be sure that the second loop is getting output after the first. Any thoughts?
Here are my loops(I excluded args because of length)
<?php
$queryOne = new WP_Query($args);
$queryTwo = new WP_Query($args2);
$results = new WP_Query();
$results->posts = array_merge($queryOne->posts, $queryTwo->posts);
?>
<?php foreach($results->posts as $post) : ?>
<?php setup_postdata( $post ); ?>
<?php get_template_part( 'property-listing' ); ?>
<?php endforeach; ?>

as parse_query relies on post_count you'll have to add the two post_counts. In your example post_count is not set. It should work if you populate the post_count. Simply add this at the end:
$results->post_count = $queryOne->post_count + $queryTwo->post_count;
Your complete example:
<?php
$queryOne = new WP_Query($args);
$queryTwo = new WP_Query($args2);
$results = new WP_Query();
$results->posts = array_merge($queryOne->posts, $queryTwo->posts);
$results->post_count = $queryOne->post_count + $queryTwo->post_count;
foreach($results->posts as $post) :
setup_postdata( $post );
get_template_part( 'property-listing' );
endforeach;
?>

Related

ACF // Cannot display sub_fields from a repeater

I created a custom taxonomy "collections". Collections items are available on the page via a relationship field.
I can access the collections and display the collections title. I'm also trying to display the data from a repeater but cannot find a way to access and display the repeater sub_fields.
When I do a var_dump($piecesList), I have an array showing the datas. How can I display these datas???
Here's my code:
<?php
$collections = get_field('collectionSelector'); /* relationship field */
if( $collections ):
foreach( $collections as $collections_post ):
$piecesList = get_field( 'pieces', $collections_post->ID );
if( $piecesList ):
foreach( $piecesList as $piecesList_post ):
$pieceName = get_field( 'piece_name', $piecesList_post->ID );
echo $pieceName;
endforeach;
endif;
endforeach;
endif;
?>

How can I echo out an Item's price in Wordpress/ Woocmerce?

lets say I am in the Page section where you put the content in WYSIWYG, my plan is to display one woocomerce item's price using php to pull out the info. I downloaded this Plugin Insert PHP where it allows you to put php code in the WYSIWYG section using a special shortcode. The thing is that I am not an expert on php whatsoever, so how would I even start?
so far I've researched this:
<?php echo $product->get_price_html(); ?>
lets say its the item with an SKU: 6-501
how would you do an php to pull the out the price ONLY of that item?
Use the get_price() method instead:
<?php echo $product->get_price(); ?>
WooCommerce version 2.3 and up has a function wc_get_product_id_by_sku( $sku ).
so you could do something like,
$sku = '6-501';
$_product_id = wc_get_product_id_by_sku( $sku );
$_product = new WC_Product( $_product_id );
echo $_product->get_price_html();
Assuming that you are using Insert PHP plugin, Place the following code between [insert_php] and [/insert_php]:
$sku = '6-501';
global $wpdb;
$id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
if ( $id ){
$product = new WC_Product( $id );
echo $product->get_price();
}
That should work.

Wordpress Trim Words in Content in Posts Archive With HTML Formatting

I'm trying to limit the number of Words in a repeating posts/posts archive. However, when I trim the words WordPress seems to remove all the formatting - the word limit works fine. Here is what I have so far:
<?php
// trim the content
global $post;
$my_content= $post->post_content;
//$my_content = get_the_content();
trimmed_my_content = wp_trim_words( $my_content, 400, ' <span class="moretext">More</span>' );
//echo $trimmed_my_content;
echo apply_filters('trimmed_my_content', $trimmed_my_content);
?>
You will see from the // out PHP that I have tried a couple of approaches with no success. However, if I just output the content in the loop with no filtering it works fine:
<?php the_content('Read more...'); ?>
Works with HTML filtering/formatting.
I should note I want to include this code in my WordPress template as I am limiting the content/excerpt elsewhere and I don't want it to be universal. Obviously, a function in functions.php called by PHP in the template is fine.
You can use
$my_content = apply_filters('the_content', $post->post_content);
Instead of:
$my_content = $post->post_content;
<?php
global $post;
//$my_content= $post->post_content;
$my_content=apply_filters('the_content',$post->post_content);//this will get the content for you
trimmed_my_content = wp_trim_words( $my_content, 400, ' <span class="moretext">More</span>' );
echo $trimmed_my_content;
?>
Or you can now format your data as required.

How to display a div in a post in your category page - wordpress

I have a website where each post is displayed and each post has a description explaining the post.
How can I get my category page (the page that summarises all posts) to show only the small summary of the post and nothing else. At the moment I am using
the_excerpt();
but this limits the text to 55 words, however some are smaller than that, and so other text is displayed in the category that I don’t want.
For example, this is an example post:
http://thestudentbubble.com/hidden-gems/hidden-gems-restaurants/ganges-indian/
As you can see below the description is additional information which I don't want it to appear on the category page.
this is the category page - http://thestudentbubble.com/hidden-gems/hidden-gems-restaurants/
yet the additional information that I don't want to appear still appears.
The text I want to appear is in:
<div class = "bioText">
How can I make only the text in the div appear in the category page?
Thank you for the help!
this is the category loop :
<div class="comment">
<?php
$cats = get_the_category();
$category_id = $cats[0]->cat_ID;
$args = array( 'posts_per_page' => 1, 'category' => $category_id);
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
$this_post_ID = get_the_ID();
$this_post_preview = get_post_meta( $this_post_ID, 'post_preview', true );
echo $this_post_preview;
//the_excerpt();
//the_content();
wp_reset_postdata();
endforeach;
?>
</div>
Ok then, the first thing to do is to create a custom textarea in each post's admin panel. It will appear under the editor. Let's create the .php file that will contain our code, call it post_preview_field.php, and place it in the theme root folder. After doing that, we will need to reference it in our functions.php file. This is done by simply adding the following line:
require_once(get_template_directory().'/post_preview_field.php');
Let's open our post_preview_field.php file and write the code.
<?php
add_action( 'add_meta_boxes', 'post_preview_metabox' );
add_action( 'save_post', 'post_preview_save_postdata' );
function post_preview_metabox() {
$screens = array( 'post' );
foreach ($screens as $screen) {
add_meta_box(
'post_preview_metabox_id',
__( 'Post Preview', 'post_preview_textdomain'),
'post_preview_inner_boxes',
$screen
);
}
}
function post_preview_inner_boxes( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'post_preview_noncename' );
$post_preview_value = get_post_meta( $post->ID, 'post_preview', true );
echo '<label for="post_preview_new_field">';
_e("", 'post_preview_textdomain' );
?>
</label>
<textarea name="post_preview_new_field" id="post_preview_new_field" ><?php echo $post_preview_value; ?></textarea>
<?php }
function post_preview_save_postdata( $post_id ) {
if ( 'post' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return;
} else {}
if ( ! isset( $_POST['post_preview_noncename'] ) || ! wp_verify_nonce( $_POST['post_preview_noncename'], plugin_basename( __FILE__ ) ) )
return;
$post_ID = $_POST['post_ID'];
$post_preview = ( $_POST['post_preview_new_field'] );
add_post_meta($post_ID, 'post_preview', $post_preview, true) or
update_post_meta($post_ID, 'post_preview', $post_preview);
}
?>
Here we go. We just created a textarea field that appears in every post's panel, can contain some text, is saved every time we save\update the post and is stored in the post's metadata.
Now, all we have to do is access this value in the frontend and echo it. How do we do that?
In the loop, we first assign the post ID to a variable:
$this_post_ID = get_the_ID();
and then assign the data we stored to another:
$this_post_preview = get_post_meta( $this_post_ID, 'post_preview', true );
That's it. We can now echo the preview:
echo $this_post_preview;
I managed to solve it by adding
<!--more-->
after each of the descriptions.

How to echo specific mysql data in specific places using php?

If the 'SELECT' statement is used to select data from a database then how do we echo specific rows to specific places on a page using php?
To explain this better - I am trying to SELECT * ALL FROM a table but to echo multiple rows to particular places on the html page using php.
So, imagine that my entire mark up and css has 20 thumbnails on a page and each thumbnail has data and an image that is unique to each thumbnail....do I have to replicate the below 20 times?
I am thinking that the best way to do this (which is probably completely wrong) is to use this statement
SELECT * FROM name_of_table WHERE ID = 4 >>> i.e. where I'd like that specific data echoed....
So, if I have 20 thumbnails do I do this 20 times?
<?php
// Connects to your Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM name_of_table WHERE ID = 4;")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['name'] . "</td> ";
Print "<th>Product:</th> <td>".$info['product_name'] . " </td></tr>";
}
Print "</table>";
?>
And, rinse and repeat but I change the below statement each time for each thumbnail (each thumbnail has unique data that comes from each row on the MySQL)
SELECT * FROM name_of_table WHERE ID = 4;
What is the best way of doing this?
Thanks!
Simple example.. First get the data with wanted ID:s. Create function for data request.
<?php
// Connects to your Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM name_of_table WHERE ID IN (2,3,4,5,6);")
or die(mysql_error());
// This holds all data rows
$data_array = array();
while($info = mysql_fetch_array( $data ))
$data_array[] = $data;
// Function for rendering data to html
function getItemHtml($id) {
$html = "";
foreach($data_array as $row) {
if ($row['ID'] == $id) {
$html = "<td>" . $row['title'] . "</td>";
// etc.. create item html here
break;
}
}
return $html;
}
// To create one item just call this with item id.
echo getItemHtml(4);
?>