I am creating a blog and I have difficulty with pages in wordpress, in my case I would like to insert content (raw content) from two pages randomly in modal system while people browse the blog.
Example:
Current Page> Modal Box> Page01 content or Page02 Content
I know it's possible by the page id. but I would like to make it more dynamic by getting the contents of the pages by slugs or by title.
here's the code
<?php
$term = get_taxonomy( $slug );
$args = array(
'post_type' => 'page',
'posts_per_page' => 1,
'orderby' => 'RAND',
'tax_query' => array( array(
'taxonomy' => $term,
'field' => 'slug',
'terms' => array('page-01', 'page-02'),
) )
);
$rand = new WP_Query($args);
if ($rand->have_posts()) {
while ($rand->have_posts()) {
$rand->the_post();
the_content();
}
}
?>
I know that removing taxonomy from query gets the contents of publish pages but, in my case, I need to get content from two specific pages
You can use post_name__in to filter by multiple slugs, eg:
$args = array(
'post_name__in' => array('page-slug-1', 'page-slug-2'),
'post_type' => 'page',
'posts_per_page' => 1,
'orderby' => 'RAND',
);
You can find more details on the WP_Query documentation page.
Related
How do I include these post types on a single query?
events -> all
guides -> all
page -> only topnews tax
This is the query I use to list posts from events and guides custom post type:
$args = array(
'post_type' => array('events', 'guides'),
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 5,
'post_status' =>'publish',
);
$newlist = get_posts( $args );
Now, I want to add post type page with a tax_query of topnews on the $args. I already registered taxonomy called property for page post type, and topnews is one of its categories.
'tax_query' => array(
array (
'taxonomy' => 'property',
'field' => 'slug',
'terms' => 'topnews',
)
),
I want to include this on the query but the events and guides post types won't show any results. Only page with topnews tax is showing.
How do I include all of this on a single query?
use
$query1 = WP_Query(); // query without taxonomy
$query2 = WP_Query(); // query with taxonomy
$q1_posts = $query1->posts;
$q2_posts = $query2->posts;
$merged_posts = array_merge($q1_posts,$q2_posts);
I'm customizing the admin of wordpress and I've created a new custom input field for the tags of a post.
It's a dropdown list with all the categories of a post. Now, I wanna find all the tags with an specific category. So, I'm using this:
$args = array(
'meta_key' => 'project',
'meta_value' => $idProject,
);
$allTags = get_tags( $args );
Is this the right way to get all the tags that I want? The problem is that the array list with the tags is coming with only one result. It should bring 2 tags with the project id that I'm passing on the variable $args.
Am I missing something?
I found a solution:
$args = array(
'hide_empty' => false, // also retrieve terms which are not used yet
'meta_query' => array(
array(
'key' => 'project',
'value' => $idProject
)
)
);
$terms = get_terms( 'post_tag', $args );
I have a custom post type with multiple attributes within it,
such as price
color make model, etc.
Which is would like Wordpress to print the amount of posts within them.
my current code is this
// Get total number of posts in "vehiclestock" post type
$count_vehiclestock = wp_count_posts('listings');
$total_vehiclestock = $count_vehiclestock->publish;
echo $total_vehiclestock . ' listings. ';
I've attached some examples to better explain myself.
as I am unaware of what to search for to answer my own question
Add This Code for count all post or category wise count post
$args = array(
'posts_per_page' => -1,
'post_type'=> 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'category_slug'
)
),
);
$wp_query = new WP_Query( $args );
$post_count = $wp_query->post_count;
I'd like to be able to call the user-selected tag ID for the Jetpack Featured Content module, but I can't find a function or an object that holds that value.
I'm basically trying to filter featured posts out of a WP_Query with these arguments:
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => $recent_posts_count,
'tag__not_in' => array(
[HERE IS WHERE I WANT PHP TO TELL ME THE TAG ID]
),
'post_type' => array(
'post'
)
);
Check This one
<?php
global $post;
$tags = get_tags($post->ID);
$tagids = array();
foreach($tags as $tag) $tagids[] = $tag->term_id;
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => $recent_posts_count,
'tag__not_in' => $tagids,
'post_type' => array(
'post'
)
);
?>
Thanks
I know this is pretty late, but I found this post while looking for the same thing, even posted in the WordPress support forum, but figured it out eventually:
$featured_options = get_option( 'featured-content' );
$featured_name = $featured_options[ 'tag-name' ];
I'm trying to do a query with the arguments below. Somehow, WP just doesn't return any posts. I can't figure out what I'm doing wrong! Any help?
Some more info: I have a custom post type that contain featured images. I want them to be displayed in a header slider. Through Advanced Custom Fields plugin I've created a custom field in the posts: 'assigned_page'. It's an array with page ID's on which that specific slide should be displayed. '$current_page' is the ID of the current page that's to be displayed. So, $args should filter the custom post type, and the posts that have the current page ID in their 'assigned_page' array.
// Get the current page ID
global $post;
$current_page = $post->ID;
$string_page = (string)$current_page;
$current_parent_page = $mv_is_subpage->ID;
// Post selection
$args = array (
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'order' => $order,
'no_found_rows' => 1,
'meta_query' => array(
array(
'meta_key' => 'assigned_page',
'meta_value' => $string_page,
)
),
);
Then:
$query = new WP_Query( $args );
And then the loop:
while ($query->have_posts()) : $query->the_post();
I guess it should be like this (according to docs at http://codex.wordpress.org/Class_Reference/WP_Query) :
array(
'key' => 'assigned_page',
'value' => $string_page,
)