WP Loop with custom array field as argument - php

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,
)

Related

filter posts query by post object value

I have a custom post type with a post object field that enables the selection of related pages for the custom post type, the post object field is enabled for multiple posts, and is set to return post id.
I want to build a query in a specific page that checks all the custom post types that are related to this page with post object.
I am using this query:
$val = get_the_ID(); // get current page ID
$args = array(
'post_type' => 'articles_content', //custom post type
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'pages_link', //the name of the post object field
'value' => $val, //the value of the current ID
'compare' => 'LIKE'
),
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) :
$the_query->the_post();
// Do something...
endwhile;
endif;
wp_reset_query();
The problem is this - for examlpe if my page ID is "80", then the query also brings custom posts that are have pages with ID of "8002" or something similar.
I tried to use the "=" sign in the compare but got no results at all.
How can I query only by the custom posts that have the exact page ID as the current page in their post object array values?
This is the solution if anyone encounters the same issue:
$args = array(
'posts_per_page' => -1,
'post_type' => 'articles_content',
'meta_query' => array(
array(
'key' => 'pages_link',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
);

Get page content by slug or title wordpress

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.

How to query taxonomy for a post type then include another post type with no tax

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);

Find tags by custom field value - Wordpress

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 );

Wordpress Jetpack Featured Content Tag ID

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' ];

Categories